Tom*_*ren 63 iphone objective-c uilabel
我正在寻找一种方法来使用UILabel(或类似的东西)来显示这样的东西:
汤姆:有些消息.
这就像在Facebook应用程序中显示"你在想什么?"的方式.消息.有没有人有任何建议如何处理这个?
Aks*_*hay 103
有一种方法可以使用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 *verdanaDict = [NSDictionary dictionaryWithObject:VerdanaFont forKey:NSFontAttributeName];
NSMutableAttributedString *vAttrString = [[NSMutableAttributedString alloc]initWithString: newsDate attributes:verdanaDict];
[vAttrString addAttribute:NSForegroundColorAttributeName value:[UIColor blackColor] range:(NSMakeRange(0, 15))];
[aAttrString appendAttributedString:vAttrString];
lblText.attributedText = aAttrString;
Run Code Online (Sandbox Code Playgroud)
请注意,lblText是UILabel,outlet作为文件所有者.人们可以继续追加他想要的NSMutableAttributedString.
另请注意,我在项目中添加了verdana和arial字体,并为此添加了一个plist.
Abd*_*sin 26
对不起,迟到了.下面的代码非常适合我.我发布这个,以便它可以对某人有所帮助.
UIFont *font1 = [UIFont fontWithName:kMyriadProSemiBold size:15];
NSDictionary *arialDict = [NSDictionary dictionaryWithObject: font1 forKey:NSFontAttributeName];
NSMutableAttributedString *aAttrString1 = [[NSMutableAttributedString alloc] initWithString:@"My" attributes: arialDict];
UIFont *font2 = [UIFont fontWithName:kMyriadProRegular size:15];
NSDictionary *arialDict2 = [NSDictionary dictionaryWithObject: font2 forKey:NSFontAttributeName];
NSMutableAttributedString *aAttrString2 = [[NSMutableAttributedString alloc] initWithString:@"Profile" attributes: arialDict2];
[aAttrString1 appendAttributedString:aAttrString2];
myProfileLabel.attributedText = aAttrString1;
Run Code Online (Sandbox Code Playgroud)
请注意我的是半成品和个人资料是正常的.我使用过MyRiad字体.谢谢
mah*_*udz 18
根据需要使用两个UILabel IBOutlets,每个都有不同的格式(字体/颜色/等).根据第一个文本结束的位置,将第二个移到第一个上面.你可以通过sizeWithFont得到它:forWidth:lineBreakMode:
或者,您可以继承UILabel,并在drawRect中自己绘制文本.如果你这样做,只需添加一个实例变量来告诉你要用一种格式绘制多少字符串,然后在另一种格式中绘制其余的字符串.
更新:请参阅下面的@ Akshay的回复.从iOS6开始,UILabel可以包含NSMutableAttributedString.当我写这篇文章时,这是不可用的.
Jas*_*son 18
更新:如果你是iOS 6+,那么使用UILabel.attributedText - 否则....
我创建了这个基本的UIView子类来支持类似的功能.
它不支持的事物列表比它的更长,但基本上它允许您管理单行 UILabel并根据需要格式化.例如,这允许我在行的中间插入不同颜色的文本,并避免使用重量级的UIWebView.
我通过在我的界面中放置一个UIView对象(使用Interface Builder)并将IB中的对象类型设置为MultipartLabel来创建这些对象.然后在代码中我根据需要调用updateNumberOfLabels和各种setText选择器.
// MultipartLabel.m
// MultiLabelLabel
//
// Created by Jason Miller on 10/7/09.
// Copyright 2009 Jason Miller. All rights reserved.
//
#import "MultipartLabel.h"
@interface MultipartLabel (Private)
- (void)updateLayout;
@end
@implementation MultipartLabel
@synthesize containerView;
@synthesize labels;
-(void)updateNumberOfLabels:(int)numLabels;
{
[containerView removeFromSuperview];
self.containerView = nil;
self.containerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)] autorelease];
[self addSubview:self.containerView];
self.labels = [NSMutableArray array];
while (numLabels-- > 0) {
UILabel * label = [[UILabel alloc] initWithFrame:CGRectZero];
[self.containerView addSubview:label];
[self.labels addObject:label];
[label release];
}
[self updateLayout];
}
-(void)setText:(NSString *)text forLabel:(int)labelNum;
{
if( [self.labels count] > labelNum && labelNum >= 0 )
{
UILabel * thisLabel = [self.labels objectAtIndex:labelNum];
thisLabel.text = text;
}
[self updateLayout];
}
-(void)setText:(NSString *)text andFont:(UIFont*)font forLabel:(int)labelNum;
{
if( [self.labels count] > labelNum && labelNum >= 0 )
{
UILabel * thisLabel = [self.labels objectAtIndex:labelNum];
thisLabel.text = text;
thisLabel.font = font;
}
[self updateLayout];
}
-(void)setText:(NSString *)text andColor:(UIColor*)color forLabel:(int)labelNum;
{
if( [self.labels count] > labelNum && labelNum >= 0 )
{
UILabel * thisLabel = [self.labels objectAtIndex:labelNum];
thisLabel.text = text;
thisLabel.textColor = color;
}
[self updateLayout];
}
-(void)setText:(NSString *)text andFont:(UIFont*)font andColor:(UIColor*)color forLabel:(int)labelNum;
{
if( [self.labels count] > labelNum && labelNum >= 0 )
{
UILabel * thisLabel = [self.labels objectAtIndex:labelNum];
thisLabel.text = text;
thisLabel.font = font;
thisLabel.textColor = color;
}
[self updateLayout];
}
- (void)updateLayout {
int thisX = 0;
// TODO when it is time to support different sized fonts, need to adjust each y value to line up baselines
for (UILabel * thisLabel in self.labels) {
CGSize size = [thisLabel.text sizeWithFont:thisLabel.font
constrainedToSize:CGSizeMake(9999, 9999)
lineBreakMode:thisLabel.lineBreakMode];
CGRect thisFrame = CGRectMake( thisX, 0, size.width, size.height );
thisLabel.frame = thisFrame;
thisX += size.width;
}
}
- (void)dealloc {
[labels release];
labels = nil;
[containerView release];
containerView = nil;
[super dealloc];
}
@end
Run Code Online (Sandbox Code Playgroud)
Raj*_*ari 14
在swift 2.0中,这可以如下完成
//Defining fonts of size and type
let firstfont:UIFont = UIFont(name: "Helvetica Neue", size: 17)!
let boldFont:UIFont = UIFont(name: "HelveticaNeue-Bold", size: 17)!
let thirdFont:UIFont = UIFont(name: "HelveticaNeue-ThinItalic", size: 17)!
//Making dictionaries of fonts that will be passed as an attribute
let firstDict:NSDictionary = NSDictionary(object: firstfont, forKey:
NSFontAttributeName)
let boldDict:NSDictionary = NSDictionary(object: boldFont, forKey:
NSFontAttributeName)
let thirdDict:NSDictionary = NSDictionary(object: thirdFont, forKey:
NSFontAttributeName)
let firstText = "My name is "
let attributedString = NSMutableAttributedString(string: firstText,
attributes: firstDict as? [String : AnyObject])
let boldText = "Rajan"
let boldString = NSMutableAttributedString(string:boldText,
attributes:boldDict as? [String : AnyObject])
let finalText = " iOS"
let finalAttributedString = NSMutableAttributedString(string:
finalText, attributes: thirdDict as? [String : AnyObject])
attributedString.appendAttributedString(boldString)
attributedString.appendAttributedString(finalAttributedString)
myLabel.attributedText = attributedString
Run Code Online (Sandbox Code Playgroud)
编辑
Swift 3.0
let firstfont:UIFont = UIFont(name: "Helvetica Neue", size: 17)!
let boldFont:UIFont = UIFont(name: "HelveticaNeue-Bold", size: 17)!
let thirdFont:UIFont = UIFont(name: "HelveticaNeue-ThinItalic", size: 17)!
//Making dictionaries of fonts that will be passed as an attribute
let firstDict:NSDictionary = NSDictionary(object: firstfont, forKey:
NSFontAttributeName as NSCopying)
let boldDict:NSDictionary = NSDictionary(object: boldFont, forKey:
NSFontAttributeName as NSCopying)
let thirdDict:NSDictionary = NSDictionary(object: thirdFont, forKey:
NSFontAttributeName as NSCopying)
let firstText = "My name is "
let attributedString = NSMutableAttributedString(string: firstText,
attributes: firstDict as? [String : AnyObject])
let boldText = "Rajan"
let boldString = NSMutableAttributedString(string:boldText,
attributes:boldDict as? [String : AnyObject])
let finalText = " iOS"
let finalAttributedString = NSMutableAttributedString(string:
finalText, attributes: thirdDict as? [String : AnyObject])
attributedString.append(boldString)
attributedString.append(finalAttributedString)
myLabel.attributedText = attributedString
Run Code Online (Sandbox Code Playgroud)
编辑
Swift 4.0
let firstfont:UIFont = UIFont(name: "Helvetica Neue", size: 17)!
let boldFont:UIFont = UIFont(name: "HelveticaNeue-Bold", size: 17)!
let thirdFont:UIFont = UIFont(name: "HelveticaNeue-ThinItalic", size: 17)!
//Making dictionaries of fonts that will be passed as an attribute
let firstDict:NSDictionary = NSDictionary(object: firstfont, forKey:
NSAttributedString.Key.font as NSCopying)
let boldDict:NSDictionary = NSDictionary(object: boldFont, forKey:
NSAttributedString.Key.font as NSCopying)
let thirdDict:NSDictionary = NSDictionary(object: thirdFont, forKey:
NSAttributedString.Key.font as NSCopying)
let firstText = "My name is "
let attributedString = NSMutableAttributedString(string: firstText,
attributes: firstDict as? [NSAttributedString.Key : Any])
let boldText = "Rajan"
let boldString = NSMutableAttributedString(string:boldText,
attributes:boldDict as? [NSAttributedString.Key : Any])
let finalText = " iOS"
let finalAttributedString = NSMutableAttributedString(string:
finalText, attributes: thirdDict as? [NSAttributedString.Key : Any])
attributedString.append(boldString)
attributedString.append(finalAttributedString)
myLabel.attributedText = attributedString
Run Code Online (Sandbox Code Playgroud)
这看起来像
我更新了由@Jason建议添加contentMode(文本对齐)支持的MultipartLabel.
#import <Foundation/Foundation.h>
@interface MultipartLabel : UIView {
}
@property (nonatomic,retain) UIView *containerView;
@property (nonatomic,retain) NSMutableArray *labels;
@property (nonatomic) UIViewContentMode contentMode;
- (void)updateNumberOfLabels:(int)numLabels;
- (void)setText:(NSString *)text forLabel:(int)labelNum;
- (void)setText:(NSString *)text andFont:(UIFont*)font forLabel:(int)labelNum;
- (void)setText:(NSString *)text andColor:(UIColor*)color forLabel:(int)labelNum;
- (void)setText:(NSString *)text andFont:(UIFont*)font andColor:(UIColor*)color forLabel:(int)labelNum;
@end
Run Code Online (Sandbox Code Playgroud)
// MultipartLabel.m
// MultipartLabel
//
// Created by Jason Miller on 10/7/09.
// Updated by Laurynas Butkus, 2011
// Copyright 2009 Jason Miller. All rights reserved.
//
#import "MultipartLabel.h"
@interface MultipartLabel (Private)
- (void)updateLayout;
@end
@implementation MultipartLabel
@synthesize containerView;
@synthesize labels;
@synthesize contentMode;
-(void)updateNumberOfLabels:(int)numLabels
{
[containerView removeFromSuperview];
self.containerView = nil;
self.containerView = [[[UIView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)] autorelease];
[self addSubview:self.containerView];
self.labels = [NSMutableArray array];
while (numLabels-- > 0) {
UILabel * label = [[UILabel alloc] initWithFrame:CGRectZero];
label.backgroundColor = self.backgroundColor;
[self.containerView addSubview:label];
[self.labels addObject:label];
[label release];
}
[self updateLayout];
}
-(void)setText:(NSString *)text forLabel:(int)labelNum
{
if( [self.labels count] > labelNum && labelNum >= 0 )
{
UILabel * thisLabel = [self.labels objectAtIndex:labelNum];
thisLabel.text = text;
}
[self updateLayout];
}
-(void)setText:(NSString *)text andFont:(UIFont*)font forLabel:(int)labelNum
{
if( [self.labels count] > labelNum && labelNum >= 0 )
{
UILabel * thisLabel = [self.labels objectAtIndex:labelNum];
thisLabel.text = text;
thisLabel.font = font;
}
[self updateLayout];
}
-(void)setText:(NSString *)text andColor:(UIColor*)color forLabel:(int)labelNum
{
if( [self.labels count] > labelNum && labelNum >= 0 )
{
UILabel * thisLabel = [self.labels objectAtIndex:labelNum];
thisLabel.text = text;
thisLabel.textColor = color;
}
[self updateLayout];
}
- (void)setText:(NSString *)text andFont:(UIFont*)font andColor:(UIColor*)color forLabel:(int)labelNum
{
if( [self.labels count] > labelNum && labelNum >= 0 )
{
UILabel * thisLabel = [self.labels objectAtIndex:labelNum];
thisLabel.text = text;
thisLabel.font = font;
thisLabel.textColor = color;
}
[self updateLayout];
}
- (void)updateLayout {
int thisX;
int thisY;
int totalWidth = 0;
int offsetX = 0;
int sizes[[self.labels count]][2];
int i = 0;
for (UILabel * thisLabel in self.labels) {
CGSize size = [thisLabel.text sizeWithFont:thisLabel.font constrainedToSize:CGSizeMake(9999, 9999)
lineBreakMode:thisLabel.lineBreakMode];
sizes[i][0] = size.width;
sizes[i][1] = size.height;
totalWidth+= size.width;
i++;
}
i = 0;
for (UILabel * thisLabel in self.labels) {
// X
switch (self.contentMode) {
case UIViewContentModeRight:
case UIViewContentModeBottomRight:
case UIViewContentModeTopRight:
thisX = self.frame.size.width - totalWidth + offsetX;
break;
case UIViewContentModeCenter:
thisX = (self.frame.size.width - totalWidth) / 2 + offsetX;
break;
default:
thisX = offsetX;
break;
}
// Y
switch (self.contentMode) {
case UIViewContentModeBottom:
case UIViewContentModeBottomLeft:
case UIViewContentModeBottomRight:
thisY = self.frame.size.height - sizes[i][1];
break;
case UIViewContentModeCenter:
thisY = (self.frame.size.height - sizes[i][1]) / 2;
break;
default:
thisY = 0;
break;
}
thisLabel.frame = CGRectMake( thisX, thisY, sizes[i][0], sizes[i][1] );
offsetX += sizes[i][0];
i++;
}
}
- (void)dealloc {
[labels release];
labels = nil;
[containerView release];
containerView = nil;
[super dealloc];
}
@end
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
71214 次 |
最近记录: |