开发者社区> 问答> 正文

UILabel的attributedText属性设置了CTRunDelegate占位,但是不起作用呢

screenshot
UILabel的attributedText属性设置了CTRunDelegate占位,但是不起作用呢

展开
收起
a123456678 2016-07-29 10:07:21 2820 0
2 条回答
写回答
取消 提交回答
  • 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.

    官方文档。

    2019-07-17 20:01:34
    赞同 展开评论 打赏
  •  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
    2019-07-17 20:01:34
    赞同 展开评论 打赏
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载