는 현재이
같은 그
목표 - C 버전 당신은 할 수 있습니다 PG 이미지의 구현이 당신의 필요 텍스트와 이미지를 만들어 NSTextAttachment
같은 텍스트에 연결해야합니다 사용의
@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;
스위프트 버전
당신은 사용이
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!
}
}
예처럼 PG 이미지를 만들 수 있습니다
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
결과
쉽지 않습니다. 'NSAttributedString'에는 이것에 대한 빌드가 없으므로 CoreText를 사용해야 할 수도 있습니다. 최고의 솔루션? "PG-n"에 대한 이미지가 미리 만들어져 이미지를 그린 다음 'NSTextAttachment'를 사용할 수 있고,이 "PG-13"을 "심볼"로 간주하는 특정 글꼴을 사용하거나 만들 수 있습니다 "/ 국경과 숯 ... – Larme
안녕하세요 Larme, 당신은 경계와 텍스트를 이미지로 만들고 레이블에 추가한다고 말합니다. 간단히 설명해 주시겠습니까? – manideep
몇 가지 리드/아이디어 : https://stackoverflow.com/questions/23236703/draw-text-with-a-border-and-background-for-a-nstableview-cell – Larme