- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要在我的应用程序中使用 iOS 的系统字体“Hiragino Sans W3”,但无论我选择哪种大小/样式或 UILabel
的尺寸,字体的上升和下降总是出现剪裁:
这似乎可以通过创建 UILabel
的子类并覆盖方法 textRectForBounds:limitedToNumberOfLines:
以返回“正确”值来解决。所以下面的代码...
- (CGRect)textRectForBounds:(CGRect)bounds limitedToNumberOfLines:(NSInteger)numberOfLines
{
CGRect result = [super textRectForBounds:bounds limitedToNumberOfLines:numberOfLines];
result = CGRectInset(result, 0, -5);
return result;
}
...导致上升端和下降端不再被剪裁:
我知道也可以使用外部编辑器来调整字体的升序和降序位置。 但这是系统字体,不加任何修改不应该能正常工作吗?我在这里遗漏了什么吗?
提前感谢您的回答。
最佳答案
我认为这个问题实际上归结为字体本身(“Hiragino Sans”)。使用字体编辑器可以看到字形超出了上升值和下降值,这就是 iOS 似乎假定为显示文本的垂直“边界框”。
由于缺乏更好的解决方案,我一直在使用(非常丑陋的)hack 来修改 UIFont
只读属性 ascender
和 行高
。
文件UIFont+Alignment.h
:
#import <UIKit/UIKit.h>
@interface UIFont (Alignment)
- (void)ensureCorrectFontAlignment;
@end
文件UIFont+Alignment.m
:
#import "UIFont+Alignment.h"
#import <objc/runtime.h>
#import <objc/message.h>
static NSHashTable *adjustedFontsList;
@implementation UIFont (Alignment)
- (void)ensureCorrectFontAlignment
{
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
adjustedFontsList = [NSHashTable hashTableWithOptions:NSHashTableWeakMemory];
});
@synchronized (adjustedFontsList) {
if ([adjustedFontsList containsObject:self]) {
return;
}
else if ([self.fontName containsString:@"Hiragino"]) {
SEL originalAscenderSelector = @selector(ascender);
Method originalAscenderMethod = class_getInstanceMethod([UIFont class], originalAscenderSelector);
SEL originalLineHeightSelector = @selector(lineHeight);
Method originalLineHeightMethod = class_getInstanceMethod([UIFont class], originalLineHeightSelector);
id result = method_invoke(self, originalAscenderMethod, nil);
CGFloat originalValue = [[result valueForKey:@"ascender"] floatValue];
[result setValue:@(originalValue * 1.15) forKey:@"ascender"];
result = method_invoke(self, originalLineHeightMethod, nil);
originalValue = [[result valueForKey:@"lineHeight"] floatValue];
[result setValue:@(originalValue * 1.25) forKey:@"lineHeight"];
[adjustedFontsList addObject:self];
}
}
}
@end
要应用,只需导入标题并在创建新字体实例后调用[myUIFont ensureCorrectFontAlignment]
。
关于ios - 系统字体 "Hiragino Sans"显示为剪裁的上升和下降,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44374358/
我问我任务buildDependent是否还会建立第一级依赖项目的依赖项。 其他受抚养者不是过渡依赖于最初的项目吗? 为了更好地理解我的意思:D-> A <-B <-C 项目B和D是A的第一层依赖,而
function hoverimgon(elem){ $(elem).find('.credentials-popup').slideDown(800); }
我有一个数据集,其中每一行代表某些行为的发生次数。这些列代表设定时间量的窗口。它看起来像这样: +----------+----------+----------+----------+-------
这是我在这个网站上要问的第一个问题,如果我的处理方式有误,请务必告诉我。 无论如何,在我创建的对象 (dog) 中,我有一个“实时”函数,它通过使用 javascript 的 setInterval
我的网站上有一个谷歌图表,它是一个折线图。但是我的 Y 轴数据值只会以整数形式增加,所以我不想在 Y 轴上有“4.5”值,如您所见: 如果有人能指出正确的方向,我将不胜感激! 最佳答案 您应该使用以下
我有一个错误,当显示键盘并且切换键盘顶部的预测文本框时, View 会弹出,让用户看到黑屏。发生这种情况的原因是因为我使用了 -= 运算符,每次调用此方法时都会使值复合。该方法可以连续调用多次。我正试
我知道如何通过为父级执行 position:absolute 和为子级执行 position:relative 来将 div 堆叠在 div 之上,但是我如何制作一个 div从另一个分区“升起”?我想
我正在使用最新版本的 ionic 2。我的代码有一个 里面有一个文本输入。当我尝试在 Android 上输入内容时,整个页面都会被键盘向上推。 html文件
我正在使用最新版本的 ionic 2。我的代码有一个 里面有一个文本输入。当我尝试在 Android 上输入内容时,整个页面都会被键盘向上推。 html文件
我正在尝试解析 truetype 字体以构建和存储每个字体大小的上升部分、下降部分,我正在使用 http://nodebox.github.io/opentype.js/这做得非常出色,但我不明白如何
我的布局需要 windowSoftInputMode= adjustmentRezise,以便我的按钮与键盘一起上升。但我不希望我的 BottomNagivationView 受到此影响。我该如何解决
This answer不适用于 WebView,this doesn't任何一个。所以这个问题是专门针对使用 WebView 的。下面的代码加载了一个带有文本输入字段的网页。单击文本输入字段时,会出现
我在将 onOptionsItemSelected 与 Android Studio 的默认“设置 Activity ”(扩展 AppCompatPreferenceActivity)一起使用时遇到问
我是一名优秀的程序员,十分优秀!