gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 17:47:05 24 4
gpt4 key购买 nike

我已附上标签的屏幕截图,应显示以下内容。时间戳文本附加有两幅图像和一个具有白色边框的文本。 我已经使用 NSTextAttachment 将图像附加到标签,然后我附加了必须在最后显示或在两个图像之间显示的文本,具体取决于条件。如果文本每次都在末尾出现,我可以通过再添加一个标签轻松实现这一点,但事实并非如此。我看到了很多为标签的完整文本创建边框的教程。有没有办法只为标签的特定部分创建边框标签文本。 enter image description here

最佳答案

你需要用你需要的文本制作一张图片,并作为 NSTextAttachment 附加到文本上,这里是它的实现

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

使用示例

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 版本

你可以这样制作你的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!
}
}

使用示例

        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

结果

enter image description here

关于ios - 在标签中的特定文本部分周围绘制边框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47657612/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com