我希望下划线是下面的文字像任何其他正常的下划线机制。但是,对于NSAttributed字符串,它会留下带有"g's“和"y's”的空洞。
示例:

它应该是什么样子:

如何增加下划线和标签之间的间距?
发布于 2014-12-31 02:39:00
没有办法用NSAttributedString或CoreText来控制这种行为(除了你自己画下划线)。NSAttributedString对此没有选择(和CoreText hasn't got one, either)。
在苹果系统上,第一个版本(有差距)是“预期的”行为,因为它是苹果提供的一个版本,并在整个系统中使用(以及Safari、TextEdit等应用程序)。
如果你真的,真的想要没有空格的下划线,你需要画出不带下划线的字符串,然后自己画线(我需要这样做in one of my projects and I can tell you it's hard;参见this file,搜索" underline ")。
发布于 2017-07-04 17:17:50
我添加了一行(UIView),高度为1,宽度与label相似,与UILabel的底部对齐。
let label = UILabel()
label.text = "underlined text"
let spacing = 2 // will be added as negative bottom margin for more spacing between label and line
let line = UIView()
line.translatesAutoresizingMaskIntoConstraints = false
line.backgroundColor = label.textColor
label.addSubview(line)
label.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[line]|", metrics: nil, views: ["line":line]))
label.addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:[line(1)]-(\(-spacing))-|", metrics: nil, views: ["line":line]))发布于 2018-12-03 20:37:56
您可以使用UITextView I在NSLayoutManager中添加自定义NSAttributedStringKey "customUnderline“和swizzling方法drawUnderline。
import Foundation
import SwiftyAttributes
import UIKit
private let swizzling: (AnyClass, Selector, Selector) -> Void = { forClass, originalSelector, swizzledSelector in
guard let originalMethod = class_getInstanceMethod(forClass, originalSelector),
let swizzledMethod = class_getInstanceMethod(forClass, swizzledSelector) else {
return
}
method_exchangeImplementations(originalMethod, swizzledMethod)
}
extension NSAttributedStringKey {
static var customUnderline: NSAttributedStringKey = NSAttributedStringKey("customUnderline")
}
extension Attribute {
static var customUnderline: Attribute = Attribute.custom(NSAttributedStringKey.customUnderline.rawValue, true)
}
extension NSLayoutManager {
// MARK: - Properties
static let initSwizzling: Void = {
let originalSelector = #selector(drawUnderline(forGlyphRange:underlineType:baselineOffset:lineFragmentRect:lineFragmentGlyphRange:containerOrigin:))
let swizzledSelector = #selector(swizzled_drawUnderline(forGlyphRange:underlineType:baselineOffset:lineFragmentRect:lineFragmentGlyphRange:containerOrigin:))
swizzling(NSLayoutManager.self, originalSelector, swizzledSelector)
}()
// MARK: - Functions
@objc
func swizzled_drawUnderline(forGlyphRange glyphRange: NSRange, underlineType underlineVal: NSUnderlineStyle, baselineOffset: CGFloat, lineFragmentRect lineRect: CGRect, lineFragmentGlyphRange lineGlyphRange: NSRange, containerOrigin: CGPoint) {
guard needCustomizeUnderline(underlineType: underlineVal) else {
swizzled_drawUnderline(forGlyphRange: glyphRange,
underlineType: underlineVal,
baselineOffset: baselineOffset,
lineFragmentRect: lineRect,
lineFragmentGlyphRange: lineGlyphRange,
containerOrigin: containerOrigin)
return
}
let heightOffset = containerOrigin.y - 1 + (getFontHeight(in: glyphRange) ?? (lineRect.height / 2))
drawStrikethrough(forGlyphRange: glyphRange,
strikethroughType: underlineVal,
baselineOffset: baselineOffset,
lineFragmentRect: lineRect,
lineFragmentGlyphRange: lineGlyphRange,
containerOrigin: CGPoint(x: containerOrigin.x,
y: heightOffset))
}
// MARK: - Private functions
private func needCustomizeUnderline(underlineType underlineVal: NSUnderlineStyle) -> Bool {
guard underlineVal == NSUnderlineStyle.styleSingle else {
return false
}
let attributes = textStorage?.attributes(at: 0, effectiveRange: nil)
guard let isCustomUnderline = attributes?.keys.contains(.customUnderline), isCustomUnderline else {
return false
}
return true
}
private func getFontHeight(in glyphRange: NSRange) -> CGFloat? {
let location = characterRange(forGlyphRange: glyphRange, actualGlyphRange: nil).location
guard let font = textStorage?.attribute(.font, at: location, effectiveRange: nil) as? UIFont else {
return nil
}
return font.capHeight
}
}https://stackoverflow.com/questions/27710514
复制相似问题