tbaranes / fittablefontlabel Goto Github PK
View Code? Open in Web Editor NEWUILabel (extension or subclass) that adjust the font size to fit a frame
License: MIT License
UILabel (extension or subclass) that adjust the font size to fit a frame
License: MIT License
I just added a label to the storyboard, set up the constraints so that its bounds fills the whole screen. Then, in viewDidLayoutSubviews
, I did this:
// note that the label's text is not empty string here
print(label.fontSizeThatFits(text: "00:00", maxFontSize: 500))
print(label.fontSizeThatFits(text: "00:00:00", maxFontSize: 500))
And the values printed seems good. The first is larger than the second.
But if I do this:
label.text = ""
print(label.fontSizeThatFits(text: "00:00", maxFontSize: 500))
print(label.fontSizeThatFits(text: "00:00:00", maxFontSize: 500))
It prints 17 and 17!
After looking into the source code, I think there is something wrong with the currentAttributedStringAttributes()
method. But I'm quite inexperienced in dealing with NSAttributedString
s so I can't figure out what is wrong.
Can you fix this?
Hey, i've found one new issue here.
If i use Savoye LET (one of the default fonts) , the memory leak happens.
After installing the pod, I dragged a UILabel into a view controller's view. In the identity inspector, I typed "FittableFontLabel" in the "Class" textfield. Then Xcode freezes. I can do nothing but to force quit. I tried multiple times and the same thing happens: as soon as I typed "FittableFontLabel", Xcode just freezes!
I'm using Xcode 7.3.1 and CocoaPods 1.0.0.
Any ideas why?
After adding FittableFontLabel as a package with Swift Package Manager this error comes up:
"the manifest is missing a Swift tools version specification; consider prepending to the manifest '// swift-tools-version:5.5.0' to specify the current Swift toolchain version as the lowest Swift version supported by the project; if such a specification already exists, consider moving it to the top of the manifest, or prepending it with '//' to help Swift Package Manager find it"
After job is done, give the ability to retrieve the adjusted font size. Then this value can be saved and the computation done only once.
private func multiLineSizeState(rect: CGRect, size: CGSize) -> FontSizeState { // if rect within 10 of size if rect.height < size.height + 10 && rect.height > size.height - 10 && rect.width > size.width + 10 && rect.width < size.width - 10 { return .fit } else if rect.height > size.height || rect.width > size.width { return .tooBig } else { return .tooSmall } }
Hi, rect.width > size.width + 10 && rect.width < size.width - 10, Is there any mistake here?
... would be appreciated ๐
The label that I'm using is a little special. I can't just set the text of it and call fontSizeToFit
to get the font size that fits. Can you add a method in the UILabel
extension that returns a font size of the argument text that fits the label's size.
The signature is like this:
public func fontSizeThatFits(text: String, size: CGSize = self.bounds.size) -> CGFloat
The basic idea is that I give you some text, and the method will figure out how big the text should be to just fit the size of the label.
I think you must have this as a private method, right? Can you just make it public?
Hello,
I am importing and using FittableFontLabel into my project. While it is certainly very useful, the memory usage in-app due to library is very high. My app frequently hangs(and eventually force closes) since I have imported this.
I see in What's next
that something is being done regarding this?
Thanks!
Great solution.
I've been struggling with fitable uilabel and NSAttributedString.
But i think you should provide inset option, because some kind of font will lose it apart if we leave inset = 0 and alignment = right.
Thanks
In the README, there is this sentence:
Make the text fit the label size: width and height if multilines, width only if single lines
Why do we only fit width in single line labels?
Change This:
open override func drawText(in rect: CGRect) {
let insets = UIEdgeInsets(top: topInset, left: leftInset, bottom: bottomInset, right: rightInset)
super.drawText(in: rect.inset(by: insets))
With This: (swift 4.2)
open override func drawText(in rect: CGRect) {
let insets = UIEdgeInsets(top: topInset, left: leftInset, bottom: bottomInset, right: rightInset)
super.drawText(in: UIEdgeInsetsInsetRect(rect, insets))
}
}
I added the pod to my workspace but then I get compile errors in UILabelExtension:
Cannot assign value of type '[NSAttributedStringKey : Any]' to type '[String : Any]'
Cannot subscript a value of type '[String : Any]' with an index of type 'NSAttributedStringKey'
Cannot convert value of type '[String : Any]' to expected argument type '[NSAttributedStringKey : Any]?'
What do you think about using 0.5
delta for each iteration during the binary search? (See example https://github.com/ShingoFukuyama/UILabel-FontSizeToFit)
I'm facing a use case where the font is considered .Fit
0.5pt too soon, maybe because the next iteration is considered .TooSmall
and thus my text is truncated.
Also I can't move to Swift 3.x right now, what do you think about creating a separator release cycle for 2.3 support?
Per semver, breaking changes should bump major version number (see changelog).
Thanks!
To reproduce this problem, add a FittableFontLabel to a storyboard. Set its text to be for example "Hello\nWorld\nBye\nWorld"
. Set the numberOfLines
property to either 0 or 4, the lineBreakMode
to word wrap and the height to something small. When the height is too small, the text does not fit in the label anymore and may only show "Hello" or "Hello\nWorld".
Currently working in the branch feature/swift3.
I am using an LTMorphingLabel
to display some text that I want to fill the screen with. So I call fontSizetoFit()
on the LTMorphingLabel
. And this is the result, viewed in the view hierarchy:
The selected view is the frame of the LTMorphingLabel
. As you can see, the text could have been much larger!
This only seems to be a problem on iPads though.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.