如何将UILabel的一部分直观地作为块引用?

Dou*_*ith 9 cocoa-touch objective-c uilabel ios textkit

如何使一个特定部分UILabel看起来像一个块引用,或者在文本的左侧有一条垂直线?TextKit会在这里进来吗?如果是这样,怎么样?

Mail.app执行此操作(请参阅彩色部分和侧面的行):

在此输入图像描述

如何在不使用多个UILabels的情况下复制此效果(因为我动态创建它会相当粗糙)?

kei*_*ter 6

XIB文件

使用此常规布局创建视图(XIB),如上图所示.有一个UILabel,一个UITextView和一个UIView(蓝色矩形是一个背景颜色设置的UIView).我们称之为ThreadView.xib.将标签,textview和视图作为属性连接到视图.

然后我们可以有一个方法来生成其中一个视图供我们使用,一个方法根据帖子有多少条评论/回复添加更多ThreadViews作为子视图.

+ (instancetype)threadViewWithLabelText:(NSString *)labelText
                           textViewText:(NSString *)textViewText
                                  color:(UIColor *)color
{
    ThreadView *threadView = [[[NSBundle mainBundle] loadNibNamed:@"ThreadView"
                                                            owner:self
                                                          options:nil] firstObject];
    if (threadView) {
        threadView.label.text = labelText;
        threadView.textView.text = textViewText;
        threadView.colorView.backgroundColor = color;
    }
    return threadView;
}

- (void)addCommentView:(ThreadView *)threadView
      toViewController:(UIViewController *)viewController
{
    threadView.frame = CGRectMake(self.frame.origin.x + 25,
                                  self.textView.frame.origin.y + self.textView.frame.size.height,
                                  self.frame.size.width - (self.frame.origin.x + 10),
                                  self.frame.size.height - (self.textView.frame.origin.y + self.textView.frame.size.height));
    [viewController.view addSubview:threadView];
}
Run Code Online (Sandbox Code Playgroud)

现在,在主视图控制器中,我们可以使用这两个方法调用来创建和添加这些视图:

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Load the first post
    ThreadView *originalPost = [ThreadView  threadViewWithLabelText:@"10 Some Words 2014 More Words"
                                                       textViewText:loremIpsum
                                                              color:[UIColor blueColor]];
    originalPost.frame = CGRectMake(self.view.frame.origin.x + 8,
                                    self.view.frame.origin.y + 15,
                                    self.view.frame.size.width - 8,
                                    self.view.frame.size.height - 15);
    [self.view addSubview:originalPost];

    // Load a comment post
    ThreadView *commentPost = [ThreadView threadViewWithLabelText:@"12 December 2014 Maybe A Username"
                                                     textViewText:loremIpsum
                                                            color:[UIColor greenColor]];
    [originalPost addCommentView:commentPost
                toViewController:self];
}
Run Code Online (Sandbox Code Playgroud)

这将给我们一个如下图所示的结果.这段代码可以使用一些重构/重构,但这应该让你开始.您还可以混淆使用autolayout和/或设置视图的帧.

最后结果


And*_*rea 2

如果您的目标 iOS 低于 7,您可以使用 Core Text 做类似的事情,但由于 Core Text 是一种旧的 C 不透明类型实现,我建议您使用DTCoreText
如果您使用的是 >=iOS7,您可以使用 NSAttributed 字符串和 NSXMLDocument。即使属性字符串从 3.x 开始可用,他们也只是将它们添加到 ios6 中的 UIKIT 对象中,并从根本上改变了 iOS7 中管理它们的 UIKit 行为。
NSXMLDocument 它很有帮助,因为您可以将表示它们的字符串呈现为 HTML。