小编Jam*_*iao的帖子

为什么我无法使用“usedRectForTextContainer”获取textContainer的矩形

我在这个问题中学到了 NAlexN 的答案(在 UILabel 的 NSAttributedString 中创建可点击的“链接”?),并自己编写了一个演示。然而,我很不幸地完成了这个演示。这是我的代码:(大家可以直接将以下代码复制到新项目中进行测试)

#import "ViewController.h"

@interface ViewController ()
{
    UILabel* label;

    NSTextContainer *textContainer;
    NSLayoutManager *layoutManager;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [super viewDidLoad];
    label=[[UILabel alloc]initWithFrame:CGRectMake(15, 30, 350, 300)];
    label.backgroundColor=[UIColor greenColor];
    label.userInteractionEnabled = YES;
    UITapGestureRecognizer*  tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleTapOnLabel:)];
    [tap setNumberOfTapsRequired:1];
    [label addGestureRecognizer:tap];
    [self.view addSubview:label];

    NSMutableAttributedString *attributedString =[[NSMutableAttributedString alloc] initWithString:@"String with a link" attributes:nil];
    NSRange linkRange = NSMakeRange(14, 4); // for the word "link" in the string above
    NSDictionary *linkAttributes = @{ NSForegroundColorAttributeName …
Run Code Online (Sandbox Code Playgroud)

iphone objective-c nslayoutmanager uilabel ios

4
推荐指数
1
解决办法
1365
查看次数

标签 统计

ios ×1

iphone ×1

nslayoutmanager ×1

objective-c ×1

uilabel ×1