简体   繁体   English

在标签中绘制文本特定部分周围的边框

[英]Draw the border around particular part of text in label

I have attached a screen shot of label should show following contents. 我附上了标签的屏幕截图应该显示以下内容。 Time stamp text appended by two images and again one text having white border. 时间戳文本由两个图像附加,另一个文本具有白色边框。 I've append images to label by using NSTextAttachment then i'm appending the text that has to show either at last or in between two images depends on condition. 我通过使用NSTextAttachment将图像附加到标签然后我附加必须在最后或两个图像之间显示的文本取决于条件。 With one more label I can achieve this easily if text comes every time at the end but it won't.I saw so many tutorials to create the border for complete text of label.Is there any way to create border only for particular part of label text. 如果有一个标签,我可以很容易地实现这一点,如果文本每次最后都会出现,但它不会。我看到很多教程为标签的完整文本创建边框。是否有任何方法只为特定部分创建边框标签文字。 在此输入图像描述

You need to make an image with your needed text and attach to the text as NSTextAttachment here is the implementation for that 您需要使用所需的文本制作图像并附加到文本,因为NSTextAttachment这里是实现的

Objective-C Version You can make your PG image like this Objective-C版本 您可以像这样制作PG图像

@implementation ImageHelper

+(UIImage*)imageForText:(NSString*)text{
        UILabel * lblBadge = [[UILabel alloc] initWithFrame:CGRectMake(0,0,16,16)];
        [lblBadge setTextAlignment:NSTextAlignmentCenter];
        [lblBadge setLineBreakMode:NSLineBreakByClipping];
        [lblBadge setTextColor:[UIColor darkGrayColor]];
        [lblBadge.layer setCornerRadius:2.0f];
        [lblBadge.layer setBorderWidth:1];
        [lblBadge setText:text];
        [lblBadge sizeToFit];
        [lblBadge setBounds:CGRectMake(0,0,lblBadge.bounds.size.width + 4,lblBadge.bounds.size.height)];

        UIGraphicsBeginImageContextWithOptions(lblBadge.bounds.size, NO, [UIScreen mainScreen].scale);
        [lblBadge.layer setAllowsEdgeAntialiasing:YES];
        [lblBadge.layer renderInContext:UIGraphicsGetCurrentContext()];
        UIImage * img = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
        return img;
}

@end

example of Use 使用的例子

NSMutableAttributedString * normalNameString = [[NSMutableAttributedString alloc]initWithString:@"testing "];

NSTextAttachment * attachment = [[NSTextAttachment alloc] init];
attachment.image = [ImageHelper imageForText:@"PG-13"];
attachment.bounds = CGRectMake(0, -6, attachment.image.size.width, attachment.image.size.height);
[normalNameString appendAttributedString:[NSAttributedString attributedStringWithAttachment:attachment]];

self.lblText.attributedText = normalNameString;

Swift Version Swift版本

You can make your PG image like this 您可以像这样制作PG图像

class imageHelper{
    static func pgImage(textValue:String) ->UIImage{
        let label = UILabel(frame: CGRect(x: 0, y: 0, width: 15, height: 16))
        label.lineBreakMode = .byClipping
        label.textAlignment = .center
        label.textColor = UIColor.darkGray
        label.layer.borderColor = UIColor.darkGray.cgColor
        label.layer.borderWidth = 1
        label.layer.cornerRadius = 2
        label.text = textValue
        label.sizeToFit()
        label.bounds = CGRect(x: 0, y: 0, width: label.bounds.size.width + 4, height: label.bounds.size.height)

        UIGraphicsBeginImageContextWithOptions(label.bounds.size, false, UIScreen.main.scale)
        label.layer.allowsEdgeAntialiasing = true
        label.layer.render(in: UIGraphicsGetCurrentContext()!)

        let image = UIGraphicsGetImageFromCurrentImageContext()
        UIGraphicsEndImageContext()
        return image!
    }
}

example of Use 使用的例子

        let normalNameString = NSMutableAttributedString.init(string: "testing ")

        let attachment = NSTextAttachment()
        attachment.image = imageHelper.pgImage(textValue: "PG-13")
        attachment.bounds = CGRect(x: 0, y: -6, width: (attachment.image?.size.width)!, height: (attachment.image?.size.height)!)
        normalNameString.append(NSAttributedString(attachment: attachment))

        self.lblText.attributedText = normalNameString

RESULT 结果

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM