ios - 仅在 iOS 6 上使用特定字体剪辑的标签和文本字段文本

标签 ios objective-c fonts uitextfield uilabel

我正在编写一个将 3 种字体用于 2 种语言 UI 的应用程序。对于带有希伯来字体的希伯来 UI,一切都很好,但对于英语 UI,所有标签都在 iOS6 上用英语字体剪裁在底部(在 7 上很好)。

我开始调查,发现超过 1 行文本的行距也太小了,sizeToFit , sizeThatFits:constrainToSize all 给出的高度太小,以至于该字体无法放入框架中。

首先我尝试设置clipsToBounds:NO什么也没做,我想通过硬编码将框架放大 *1.15使用 eng 字体时,但 1) 我仍然有行距问题,以及 2) 我无法让它与 UITextField 一起使用s(由于某种原因,占位符和插入符号都被剪裁但键入的文本工作正常......) - 我尝试覆盖 textRectForBounds:editingRectForBounds:并返回给定的边界 (0,10,0,10)对于 UIEdgeInsets但没有运气...

我考虑过编辑字体信息本身,但是 A 我不知道该怎么做(并且在 Google 上找不到任何信息)并且 B 找不到任何可以做到的 mac 应用程序(只有 Windows 的东西......) .另外 - 我需要这样做似乎很奇怪,因为在 iOS7 上它可以完美运行,无需调整任何帧......(我在 iOS6 和 iOS7 模拟器以及 iOS6 iPhone 5 设备和 iOS7 iPhone5 设备上测试过这个......) .

在 iOS7 上键入文本:
Typing text on iOS7

占位符iOS7:
Placeholder iOS7

在 iOS6.1 上输入文字:
Typing text on iOS6.1

iOS6.1 上的占位符:
Placeholder on iOS6.1

其他一些带有 UILabel 的地方被剪掉了:
clipped label
(这种情况在按钮上,但随处发生)

再说一遍:这只发生在 iOS6.1 而不是 7,我猜它与 SDK 从可能以某种方式从 ios6 更改为 7 的字体文件中以像素为单位读取字体大小的方式有关...

编辑:这是我为 UITextField 问题所做的变通试验:

- (CGRect) textRectForBounds:(CGRect)bounds {
return [self UIEdgeInsetsInsetRectForTextFieldBounds:bounds];
}

- (CGRect)editingRectForBounds:(CGRect)bounds {
    return [self UIEdgeInsetsInsetRectForTextFieldBounds:bounds];
}

- (CGRect)UIEdgeInsetsInsetRectForTextFieldBounds:(CGRect)bounds {
    bounds = CGRectMake(bounds.origin.x, bounds.origin.y, bounds.size.width, bounds.size.height + 5);
    return UIEdgeInsetsInsetRect(bounds, UIEdgeInsetsMake(-2, 10, -2, 10));
}

注:它写在 UITextField类别。我知道这不是要走的路,但它确实改变了插图,所以我知道代码会发生并产生影响,它只是不会像预期的那样改变占位符的农场大小。

没用...
这些也是我通过实际计算所需空间来修复标签所做的所有尝试,正如您在评论标记中看到的那样,我最终使用了硬编码解决方法:
(评论的最后一段是我尝试解决行间距问题 - 也没有奏效)
//        CGSize maxSize = CGSizeMake(self.frame.size.width, MAXFLOAT);
////        CGSize expectedLabelSize = [self.text sizeWithFont:self.font constrainedToSize:maxSize lineBreakMode:self.lineBreakMode];
////        CGRect newFrame = self.frame;
////        newFrame.size.height = expectedLabelSize.height*2;
////        self.frame = newFrame;
////        [self setClipsToBounds:NO];
//        
//        CGSize expectedLabelSize = [self.text sizeWithFont:self.font constrainedToSize:maxSize lineBreakMode:NSLineBreakByTruncatingTail];
//        
//        //adjust the label the the new height.
//        CGRect newFrame = self.frame;
//        newFrame.size.height = expectedLabelSize.height;
//        self.frame = newFrame;

//        [self setLineBreakMode:NSLineBreakByWordWrapping];
//        [self setNumberOfLines:1];
//        [self sizeToFit];

//        CGSize maxSize = CGSizeMake(self.frame.size.width, CGFLOAT_MAX);
//        CGSize requiredSize = [self sizeThatFits:maxSize];
//        self.frame = CGRectMake(self.frame.origin.x, self.frame.origin.y, requiredSize.width, requiredSize.height);

        [self changeFrameHeightTo:self.frame.size.height*1.17];
        [self changeFrameYBy:-5];

//        NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithString:self.text];
//        NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
//        [paragraphStyle setLineSpacing:10];
//        [attributedString addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [self.text length])];
//        self.attributedText = attributedString;

** changeFrame...是我为 UIView 构建的一个类别,用于更改特定值而无需使用 setFrame:CGRectMake(... *

最佳答案

为什么不根据你使用的文字和字体来计算文字大小呢?

CGSize titleSize = [title sizeWithFont:<your font here> constrainedToSize:<button size > lineBreakMode:NSLineBreakByTruncatingTail];

然后您可以相应地更改占位符的大小。

请注意,此功能在 ios 7.0 中已弃用。
对于 ios7,您需要使用:boundingRectWithSize:options:attributes:context:

关于ios - 仅在 iOS 6 上使用特定字体剪辑的标签和文本字段文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20595729/

相关文章:

android - Android 中文本的 Arial 字体

ios - 在 swift 2.0 中允许用户播放背景音乐

ios - 如何在UITableView中正确使用Enum?

ios - -[__NSCFDictionary objectAtIndexedSubscript :]: unrecognized selector sent to instance

iphone - 如何获得精确的时间,例如 Objective-C 中的毫秒数?

java - 为什么 Java Swing html 字体渲染看起来这么糟糕?

html - WebFont @font-face 定义 serif/sans-serif/monospace

ios - 带有部分的 UItabelview 内的多个 UICollectionviews - swift

iOS Xcode Swift 将输入名字和姓氏组合成全名问题

ios - UISearchController 在 iPad 上显示全屏而不是弹出窗口