UILabel的attributedText属性设置了CTRunDelegate占位,但是不起作用呢
Because an embedded object is only a display-time modification, you should avoid applying this attribute to a range of text with complex behavior, such as text having a change of “writing direction” or having combining marks.
官方文档。
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
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。