Iphone 如何更改UILabel/UIFont';字母间距是多少?
我已经搜索了很多东西,但找不到答案 我有一个普通的UILabel,定义如下:Iphone 如何更改UILabel/UIFont';字母间距是多少?,iphone,xcode,uilabel,spacing,uifont,Iphone,Xcode,Uilabel,Spacing,Uifont,我已经搜索了很多东西,但找不到答案 我有一个普通的UILabel,定义如下: UILabel *totalColors = [[[UILabel alloc] initWithFrame:CGRectMake(5, 7, 120, 69)] autorelease]; totalColors.text = [NSString stringWithFormat:@"%d", total]; totalColors.font = [UIFont fontWithName:@"
UILabel *totalColors = [[[UILabel alloc] initWithFrame:CGRectMake(5, 7, 120, 69)] autorelease];
totalColors.text = [NSString stringWithFormat:@"%d", total];
totalColors.font = [UIFont fontWithName:@"Arial-BoldMT" size:60];
totalColors.textColor = [UIColor colorWithRed:221/255.0 green:221/255.0 blue:221/255.0 alpha:1.0];
totalColors.backgroundColor = [UIColor clearColor];
[self addSubview:totalColors];
我希望字母之间的水平间距更紧,同时保持字体大小
有办法做到这一点吗?这应该是一件非常基本的事情
干杯,伙计们,
安德烈
更新:
所以我不得不这样做:
- (void) drawRect:(CGRect)rect
{
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSelectFont (context, "Arial-BoldMT", 60, kCGEncodingMacRoman);
CGContextSetCharacterSpacing (context, -10);
CGContextSetTextDrawingMode (context, kCGTextFill);
CGContextSetRGBFillColor(context, 221/255.0, 221/255.0, 221/255.0, 221/255.0);
CGAffineTransform xform = CGAffineTransformMake(1.0, 0.0, 0.0, -1.0, 0.0, 0.0);
CGContextSetTextMatrix(context, xform);
char* result = malloc(17);
sprintf(result, "%d", totalNumber);
CGContextShowTextAtPoint (context, 0, 54, result, strlen(result));
}
但是我需要把这个对齐到右边。
如果我知道绘制文本的宽度,我可以手动完成,但事实证明,要找到它几乎是不可能的
我读过关于ATSU的书,但找不到任何例子
这太糟糕了:/在任何公开版本的iPhone操作系统中都没有如果您是当前的iPhone开发人员,您可以通过查看iPhone OS 3.2的“新功能”说明了解iPhone OS的发展方向
更新:iOS v3.2,它增加了对内核的支持,在我发布这篇文章时,它仍然处于保密协议之下。有关最新答案的更新,请参见。我已经提出了字母间距和向右对齐的解决方案 下面是:
NSString *number = [NSString stringWithFormat:@"%d", total];
int lastPos = 85;
NSUInteger i;
for (i = number.length; i > 0; i--)
{
NSRange range = {i-1,1};
NSString *n = [number substringWithRange:range];
UILabel *digit = [[[UILabel alloc] initWithFrame:CGRectMake(5, 10, 35, 50)] autorelease];
digit.text = n;
digit.font = [UIFont fontWithName:@"Arial-BoldMT" size:60];
digit.textColor = [UIColor colorWithRed:221/255.0 green:221/255.0 blue:221/255.0 alpha:1.0];
digit.backgroundColor = [UIColor clearColor];
[self addSubview:digit];
CGSize textSize = [[digit text] sizeWithFont:[digit font]];
CGFloat textWidth = textSize.width;
CGRect rect = digit.frame;
rect.origin.x = lastPos - textWidth;
digit.frame = rect;
lastPos = rect.origin.x + 10;
}
字母间距为最后一行的“10”。
对齐来自lastPos
希望这对其他人有所帮助。我扩展了UILabel以更改字符间距。这应该可以解决这个问题,并从UILabel本身提取字体、文本、颜色等(正确的编码!) 你可能会注意到我画了两次文本,第一次是用清晰的颜色。这是为了使标签中的文本自动居中。虽然这可能是低效的-自动居中不是很好吗 享受吧
@interface RALabel : UILabel {
}
@end
@implementation RALabel
- (void) drawRect:(CGRect)rect
{
// Drawing code
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSelectFont (context, [self.font.fontName cStringUsingEncoding:NSASCIIStringEncoding], self.font.pointSize, kCGEncodingMacRoman);
CGContextSetCharacterSpacing(context, 1);
CGContextSetFillColorWithColor(context, [[UIColor clearColor] CGColor]);
CGAffineTransform myTextTransform = CGAffineTransformScale(CGAffineTransformIdentity, 1.f, -1.f );
CGContextSetTextMatrix (context, myTextTransform);
// draw 1 but invisbly to get the string length.
CGPoint p =CGContextGetTextPosition(context);
float centeredY = (self.font.pointSize + (self.frame.size.height- self.font.pointSize)/2)-2;
CGContextShowTextAtPoint(context, 0, centeredY, [self.text cStringUsingEncoding:NSASCIIStringEncoding], [self.text length]);
CGPoint v =CGContextGetTextPosition(context);
// calculate width and draw second one.
float width = v.x - p.x;
float centeredX =(self.frame.size.width- width)/2;
CGContextSetFillColorWithColor(context, [self.textColor CGColor]);
CGContextShowTextAtPoint(context, centeredX, centeredY, [self.text cStringUsingEncoding:NSASCIIStringEncoding], [self.text length]);
}
在iOS 6中,您可以在UILabel中使用NSAttributedString 在属性化字符串中,可以使用属性NSKernerAttributeName设置字母间距
NSMutableAttributedString* attrStr = [[NSMutableAttributedString alloc] initWithString: @"Test test test test "];
[attrStr addAttribute:NSKernAttributeName value:@(4.0) range:NSMakeRange(0, attrStr.length)];
UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(0, 300, 300, 100)];
label.attributedText = attrStr;
迅速:
let myTitle = "my title"
let titleLabel = UILabel()
let attributes: NSDictionary = [
NSFontAttributeName:UIFont(name: "HelveticaNeue-Light", size: 20),
NSForegroundColorAttributeName:UIColor.whiteColor(),
NSKernAttributeName:CGFloat(2.0)
]
let attributedTitle = NSAttributedString(string: myTitle, attributes: attributes as? [String : AnyObject])
titleLabel.attributedText = attributedTitle
titleLabel.sizeToFit()
如果不使用自定义字体或Quartz 2D,则没有其他方法可以执行此操作?您可以通过调用-(CGSize)sizeWithFont:(UIFont*)font minFontSize:(CGFloat)minFontSize actualFontSize:(CGFloat*)actualFontSize forWidth:(CGFloat)width lineBreakMode:(UILineBreakMode)NSString类别UIStringDrawing的lineBreakMode方法。然后,我猜,您可以将该大小减少文本的收缩量。如果您仍然需要它:)如果有人需要解决方案,请在此处选中它:签出以计算字距(字符之间的紧密间距属性)以适应标签的宽度。您可以使用textRectForBounds:LimitedToNumberOfflines:确定在UILabel中绘制的文本的尺寸。不过,听起来你找到了你要找的东西!我以为你在找克宁。快乐编码!截至此日期,当前的iOS版本为6.1。实现这一点(很容易)的方法是使用
NSAttributedString
,但在iOS 6.0之前,它在大多数控件中都不可用。您的方法是可行的(也是我想到的第一个),但不幸的是,如果您需要渲染多个文本区域,它可能无法正常工作。我认为Andre提供的更新在一般情况下要好得多。为了节省我的时间,您只能在这个解决方案中使用ascii字符:)(y)