【问题标题】:NSLayoutManager Delegate not called未调用 NSLayoutManager 委托
【发布时间】:2016-01-14 09:19:55
【问题描述】:

我想知道为什么我的委托方法

- (void)layoutManager:(NSLayoutManager *)layoutManager
didCompleteLayoutForTextContainer:(NSTextContainer *)textContainer
                atEnd:(BOOL)layoutFinishedFlag

没有调用,我使用了这个代码:

self.storage = [[NSTextStorage alloc] initWithString:@"sdfsdf sdf sdf sdf sdf sdf Sdf sdf sdf "];
self.layout = [[NSLayoutManager alloc] init];
[self.storage addLayoutManager:self.layout];
self.layout.delegate = self;
NSTextContainer * container = [[NSTextContainer alloc] init];
[self.layout addTextContainer:container];

TextStorage 和 LayoutManager 属性强保留在类中,但是没有调用委托方法!!!

【问题讨论】:

    标签: ios objective-c layout-manager textkit


    【解决方案1】:

    设置委托后修改 NSTextStorage 时会调用委托方法。 这是来自 UIViewController 的 sn-p:

    - (void)viewDidLoad {
        [super viewDidLoad];
        self.layout = [[NSLayoutManager alloc] init];
        self.layout.delegate = self;
        self.storage = [[NSTextStorage alloc] init];
        [self.storage addLayoutManager:self.layout];
        NSTextContainer * container = [[NSTextContainer alloc] init];
        [self.layout addTextContainer:container];
        NSAttributedString *string = [[NSAttributedString alloc] initWithString:@"New string"];
        [self.storage setAttributedString:string];
    }
    
    - (void)layoutManager:(NSLayoutManager *)layoutManager
    didCompleteLayoutForTextContainer:(NSTextContainer *)textContainer
                    atEnd:(BOOL)layoutFinishedFlag
    {
        NSLog(@"I'm here");
    }
    

    【讨论】:

      【解决方案2】:

      我发现当您将任何容器添加到 UITextView 时也会调用委托方法。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-04-06
        • 2014-03-18
        • 2019-08-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多