ios - 是否可以在 UILabel/UITextView 中添加 UIButton?

标签 ios objective-c

这是一个有趣的场景。我从服务器发送了以下文本:

I go to school by #option#

在 iOS 应用程序中,我必须将其转换为:

I go to school by [UIButton]

挑战是测量前面的文本(“I go to school by”)的长度,以便设置 UIButton 的位置(origin) >。当 UILabel 有多行,UIButton 多次出现时,问题会更严重,例如:

I go to school by #option#. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Phasellus fringilla, massa ut pharetra ultricies, elit nunc accumsan libero, ut elementum est justo sed lacus. Proin mi tellus, suscipit in magna a, auctor laoreet eros. #option# Quisque rhoncus ac mauris ac interdum. Etiam a odio augue. Mauris porttitor purus ac maximus faucibus. Suspendisse velit felis, varius lobortis turpis venenatis, tempor placerat magna. Integer in semper justo, ut pretium nunc. Aliquam laoreet vehicula finibus.#option#

以下是我能想到的一些解决方案:

方法一

NSString * testText = @"demo title";
CGSize stringSize = [testText sizeWithAttributes:@{NSFontAttributeName: [UIFont systemFontOfSize:17.0f]}];

该方法可以测量单行文本的大小,但对于多行文本,CGSize会不正确。

方法二

NSTextStorage *textStorage = [[NSTextStorage alloc] initWithString:@"hello"];
NSLayoutManager *layoutManager = [[NSLayoutManager alloc] init];
NSTextContainer *textContainer = [[NSTextContainer alloc] init];

[layoutManager addTextContainer:textContainer];
[textStorage addLayoutManager:layoutManager];

//Figure out the bounding rectangle
NSRect stringRect = [layoutManager boundingRectForGlyphRange:NSMakeRange(0, [layoutManager numberOfGlyphs]) inTextContainer:textContainer];

这要复杂得多。 (仍在弄清楚如何构建布局)

我的问题是:有没有更简单的方法来实现我的目标?

最佳答案

使用 UITextView 你可以解决你的问题只需添加下面的方法

-(void)addButtons{
    NSRange searchRange = NSMakeRange(0, [self.localTextView.text length]);
    NSLog(@"%d", self.localTextView.text.length);
    while(searchRange.location != NSNotFound){
        searchRange = [self.localTextView.text rangeOfString:@"#option#" options:NSCaseInsensitiveSearch range:searchRange];
        if (searchRange.location != NSNotFound)
        {
            NSLog(@"%d", searchRange.location);
            UITextPosition *beginning = self.localTextView.beginningOfDocument;
            UITextPosition *start = [self.localTextView positionFromPosition:beginning offset:searchRange.location];
            UITextPosition *end = [self.localTextView positionFromPosition:start offset:searchRange.length];
            UITextRange *range = [self.localTextView textRangeFromPosition:start toPosition:end];
            CGRect result1 = [self.localTextView firstRectForRange:range];
            NSLog(@"%f, %f", result1.origin.x, result1.origin.y);

            UIButton* btn = [UIButton buttonWithType:UIButtonTypeCustom];
            [btn addTarget:self action:@selector(doAction:) forControlEvents:UIControlEventTouchUpInside];
            [btn setTitle:@"Click" forState:UIControlStateNormal];
            [btn setBackgroundColor:[UIColor blackColor]];
            [btn setFrame:result1];
            [self.localTextView addSubview:btn];

            searchRange = NSMakeRange(searchRange.location + searchRange.length, self.localTextView.text.length - (searchRange.location + searchRange.length));
        }
    }
}

localTextView 是 IBOutlet 取自 Storyboard,最后只需调用 [self addButtons]; 在你的 viewDidAppear

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    [self addButtons];
}

根据需要设置按钮外观和操作方法,我只是制作了一个带有标题的黑色背景。这是我得到的输出

enter image description here

关于ios - 是否可以在 UILabel/UITextView 中添加 UIButton?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33728367/

相关文章:

ios - 在 Swift 3/iOS10 中使用带有自定义字体的动态类型的更好方法

ios - 三元运算符根据变量的值设置值

ios - UITableViewCell subview 的 IBOutlet 为 (NULL)

objective-c - 保留/分配内存管理

iphone - 对字典数组进行排序

ios - 我的应用程序在 iPhone 6 和 6plus 中不显示状态栏

ios - 非常奇怪的 NSInvalidArgumentException 与 addSubview FBLoginView

iphone - Objective-C - 基于 eclipse 刻线边界切割图像

ios - 如何使用 Objective C 获取 Tableview 选择的单元格 UILabels 值?

ios - 缩放一个 UIView,除了它的一个 subview