最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

ios - Why is my titleLabel width getting smaller as the dynamic text size gets bigger? - Stack Overflow

programmeradmin0浏览0评论

I have text on a small button that isn't important, and I want it to not change with dynamic text size. I have done that in the UIButton config, but it seems that the width of the label seems to get smaller and start to clip my text that isn't changing size. Previously it was forcing it to wrap and I could force it to clip with titleLineBreakMode but that's just a band-aid. What's going on here?

Here's my button config:

var config = UIButton.Configuration.plain()
config.titleLineBreakMode = .byClipping // Prevents word wrap, but just a band-aid
config.titleTextAttributesTransformer = UIConfigurationTextAttributesTransformer { incoming in
  var attributes = incoming
  attributes.font = .systemFont(ofSize: 16)
  return attributes
}
button.configuration = config

Here's my button with Large dynamic text size.

Here's my button with Accessibility 3 text size.

I have text on a small button that isn't important, and I want it to not change with dynamic text size. I have done that in the UIButton config, but it seems that the width of the label seems to get smaller and start to clip my text that isn't changing size. Previously it was forcing it to wrap and I could force it to clip with titleLineBreakMode but that's just a band-aid. What's going on here?

Here's my button config:

var config = UIButton.Configuration.plain()
config.titleLineBreakMode = .byClipping // Prevents word wrap, but just a band-aid
config.titleTextAttributesTransformer = UIConfigurationTextAttributesTransformer { incoming in
  var attributes = incoming
  attributes.font = .systemFont(ofSize: 16)
  return attributes
}
button.configuration = config

Here's my button with Large dynamic text size.

Here's my button with Accessibility 3 text size.

Share Improve this question asked Feb 5 at 19:27 teradylteradyl 2,8021 gold badge31 silver badges39 bronze badges 2
  • If you don't want the button's label's font changing size, try button.titleLabel.adjustsFontForContentSizeCategory = false. – HangarRash Commented Feb 5 at 19:39
  • @HangarRash That doesn't work within a UIButton Configuration as I've set it up. – teradyl Commented Feb 6 at 1:34
Add a comment  | 

1 Answer 1

Reset to default 0

rather than adding it that like do this and apply it later than it works

var config = UIButton.Configuration.plain()
config.minimumSize = CGSize(width: 100, height: 40) // Adjust as needed

let attributes: [NSAttributedString.Key: Any] = [
    .font: UIFont.systemFont(ofSize: 16)
]
let attributedTitle = NSAttributedString(string: "300 text", attributes: attributes)

let button = UIButton(configuration: config)
button.setAttributedTitle(attributedTitle, for: .normal)
button.setContentHuggingPriority(.required, for: .horizontal)
button.setContentCompressionResistancePriority(.required, for: .horizontal)
发布评论

评论列表(0)

  1. 暂无评论