【发布时间】:2016-08-10 02:20:52
【问题描述】:
【问题讨论】:
-
你可以为此使用堆栈视图并对齐顶部。
-
我的目标是 iOS 8。所以我不能使用 stackView
标签: ios autolayout uilabel
【问题讨论】:
标签: ios autolayout uilabel
我可以给你看一张这张图片的例子:
代码:
- (NSMutableAttributedString *)styleSalePriceLabel:(NSString *)salePrice withFont:(UIFont *)font
{
if ([salePrice rangeOfString:@"."].location == NSNotFound) {
return [[NSMutableAttributedString alloc] initWithString:salePrice];
} else {
NSRange range = [salePrice rangeOfString:@"."];
range.length = (salePrice.length - range.location);
NSMutableAttributedString *stylizedPriceLabel = [[NSMutableAttributedString alloc] initWithString:salePrice];
UIFont *smallFont = [UIFont fontWithName:font.fontName size:(font.pointSize / 2)];
NSNumber *offsetAmount = @(font.capHeight - smallFont.capHeight);
[stylizedPriceLabel addAttribute:NSFontAttributeName value:smallFont range:range];
[stylizedPriceLabel addAttribute:NSBaselineOffsetAttributeName value:offsetAmount range:range];
return stylizedPriceLabel;
}
}
【讨论】:
终于....我明白了
//VerticalSpace from SmallFont Label to largeFont Label
@IBOutlet weak var dollarLabelTopSpacetoAmountLabel: NSLayoutConstraint!
@IBAction func btnClicked() {
let amountString = self.amountLbl.text! as NSString
let fontSize = self.amountLbl.frame.size.width / CGFloat(amountString.length)
let difference = self.amountLbl.frame.size.height-fontSize
print(fontSize)
//Your minimum font size(30).
if fontSize>30 {
self.dollarLabelTopSpacetoAmountLabel.constant = -(difference-
}else{
// dollarLabelTopSpacetoAmountLabel.constant = -30
}
}
【讨论】: