UILabel的attributedText属性设置了CTRunDelegate占位,但是不起作用呢
代码如下:
CGFloat ascentCallback(void* refCon) {
return 20;
}
CGFloat descentCallback(void* refCon) {
return 50;
}
CGFloat widthCallback(void* refCon) {
return 100;
}
@interface ViewController ()
@property (nonatomic, strong) QQAttributedLabel *label;
@property (nonatomic, strong) NSMutableAttributedString *attributedText;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_attributedText = [[NSMutableAttributedString alloc] initWithString:@"I added kCTRunDelegateAttributeName to the attributedText, but it does not work"];
CTRunDelegateCallbacks callbacks;
memset(&callbacks, 0, sizeof(CTRunDelegateCallbacks));
callbacks.version = kCTRunDelegateVersion1;
callbacks.getAscent = ascentCallback;
callbacks.getDescent = descentCallback;
callbacks.getWidth = widthCallback;
CTRunDelegateRef delegate = CTRunDelegateCreate(&callbacks, NULL);
if (NULL != delegate) {
unichar objectReplacementChar = 0xFFFC;
NSString *objectReplacementString = [NSString stringWithCharacters:&objectReplacementChar length:1];
NSMutableAttributedString* space = [[NSMutableAttributedString alloc] initWithString:objectReplacementString];
CFRange range = CFRangeMake(0, 1);
CFMutableAttributedStringRef spaceString = (__bridge_retained CFMutableAttributedStringRef)space;
CFAttributedStringSetAttribute(spaceString, range, kCTRunDelegateAttributeName, delegate);
CFAttributedStringSetAttribute(spaceString,
range,
kCTWritingDirectionAttributeName,
(__bridge CFArrayRef)@[@(kCTWritingDirectionLeftToRight)]);
CFRelease(delegate);
CFRelease(spaceString);
[_attributedText insertAttributedString:space atIndex:30];
}
_label = [[QQAttributedLabel alloc] initWithFrame:CGRectMake(0, 0, 200, 20)];
_label.backgroundColor = [UIColor yellowColor];
_label.numberOfLines = 0;
_label.attributedText = _attributedText;
[_label sizeToFit];
_label.center = self.view.center;
[self.view addSubview:_label];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
huandi
10 years, 4 months ago