「これはいいりんごです」という文字列があります。私のUIlabelに表示します。「良い」という言葉を別のフォントで設定したいと思います。「これはいいりんごだ」みたいな顔してるだけ。
6 に答える
NSAttributedStringを見てください... UILabel、UITextViewはNSAttributedStringをサポートしていないため、OHAttributedLabel draw NSAttributedStringを使用してください...
PS: iOS6 以降のアプリケーションを配布する場合は、UILabel が NSAttributedString をサポートするようになったため、OS でネイティブにサポートされるようになったため、OHAttributedLabel の代わりに UILabel を直接使用する必要があります。
NSMutableAttributedString を使用して、Label に異なる/複数のフォントとその他のプロパティを設定する方法があります。Follは私のコードです:
UIFont *ArialFont = [UIFont fontWithName:@"arial" size:18.0];
NSDictionary *arialdict = [NSDictionary dictionaryWithObject: ArialFont forKey:NSFontAttributeName];
NSMutableAttributedString *AattrString = [[NSMutableAttributedString alloc] initWithString:title attributes: arialdict];
UIFont *VerdanaFont = [UIFont fontWithName:@"verdana" size:12.0];
NSDictionary *veradnadict = [NSDictionary dictionaryWithObject:VerdanaFont forKey:NSFontAttributeName];
NSMutableAttributedString *VattrString = [[NSMutableAttributedString alloc]initWithString: newsDate attributes:veradnadict];
[VattrString addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:(NSMakeRange(0, 15))];
[AattrString appendAttributedString:VattrString];
lblText.attributedText = AattrString;
lblText は UILabel であり、ファイル所有者としてのアウトレットであることに注意してください。
必要な数の NSMutableAttributedString を追加し続けることができます..
また、プロジェクトに verdana と arial フォントを追加し、同じ plist を追加したことに注意してください。
2 つの UILabels を使用する代わりに、1 つのラベルに複数のテキスト フォント スタイルと色を設定できます。例を次に示します。
UILabel *customLbl = [[UILabel alloc] initWithFrame:CGRectMake(50, 100, 200, 25)];
customLbl.backgroundColor = [UIColor yellowColor];
[self createTwoTextStyleInSingleUILabel:customLbl];// custom method calling
[self.view addSubview:customLbl];
カスタム メソッドは次のようになります。このメソッドを適用する前に、QuartzCore フレームワーク (CALayers に必要) と CoreText フレームワーク (属性付き文字列に必要) をプロジェクトに追加します。
#import <QuartzCore/QuartzCore.h>
#import <CoreText/CoreText.h>
- (void)createTwoTextStyleInSingleUILabel: (UILabel *) myLabel{
NSString *firstText = NSLocalizedString(@"First text:", nil);
NSString *secondText = [NSString stringWithFormat:@"%@ %@",firstText,@"Second text"];
CATextLayer *myLabelTextLayer;
/* Create the text layer on demand */
if (!myLabelTextLayer) {
myLabelTextLayer = [[CATextLayer alloc] init];
myLabelTextLayer.backgroundColor = [UIColor clearColor].CGColor;
myLabelTextLayer.wrapped = NO;
CALayer *layer = myLabel.layer; //assign layer to your UILabel
myLabelTextLayer.frame = CGRectMake((layer.bounds.size.width-180)/2 + 10, (layer.bounds.size.height-30)/2 + 10, 180, 30);
myLabelTextLayer.contentsScale = [[UIScreen mainScreen] scale];
myLabelTextLayer.alignmentMode = kCAAlignmentCenter;
[layer addSublayer:myLabelTextLayer];
}
/* Create the attributes (for the attributed string) */
// customizing first string
CGFloat fontSize = 16;
UIFont *boldFont = [UIFont boldSystemFontOfSize:fontSize];
CTFontRef ctBoldFont = CTFontCreateWithName((__bridge CFStringRef)boldFont.fontName, boldFont.pointSize, NULL);
CGColorRef cgColor = [UIColor redColor].CGColor;
NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:
(__bridge id)ctBoldFont, (id)kCTFontAttributeName,
cgColor, (id)kCTForegroundColorAttributeName, nil];
CFRelease(ctBoldFont);
// customizing second string
UIFont *font = [UIFont systemFontOfSize:16];
CTFontRef ctFont = CTFontCreateWithName((__bridge CFStringRef)font.fontName, font.pointSize, NULL);
CGColorRef cgSubColor = [UIColor blackColor].CGColor;
NSDictionary *subAttributes = [NSDictionary dictionaryWithObjectsAndKeys:(__bridge id)ctFont, (id)kCTFontAttributeName,cgSubColor, (id)kCTForegroundColorAttributeName, nil];
CFRelease(ctFont);
/* Create the attributed string (text + attributes) */
NSMutableAttributedString *attrStr = [[NSMutableAttributedString alloc] initWithString:secondText attributes:attributes];
float lengthOfSecondString = 12.0; // length of second string including blank space inbetween text, space in front , space after text.. Be careful, your app may crash here if length is beyond the second text length (lengthOfSecondString = text length + blank spaces)
[attrStr addAttributes:subAttributes range:NSMakeRange(firstText.length, lengthOfSecondString)];
// you can add another subattribute in the similar way as above , if you want change the third textstring style
/* Set the attributes string in the text layer :) */
myLabelTextLayer.string = attrStr;
myLabelTextLayer.opacity = 1.0; //to remove blurr effect
}
申し訳ありませんが、UILabel の実装ではそれができません。次の 2 つのオプションがあります。
- UIWebView を作成し、HTML 形式のテキストを配置できます
- たとえば、 Core Textを使用して、その機能を備えた独自のUILabel実装を作成できます
これは UILabel では不可能です。ただし、 Core Textを使用できます。
他の簡単な方法は、非常に小さな UIWebView を使用することです。これで、html コマンドを使用してフォントを設定できます。