Exceeding max Text() concatenation length - SwiftUI -
Hmm... unexpected limitation... anyway - learn something new.
Ok, here is improved algorithm, which should move that limitation far away.
Tested with Xcode 12 / iOS 14. (also updated code in referenced topic Highlight a specific part of the text in SwiftUI)
func hilightedText(str: String, searched: String) -> Text {
guard !str.isEmpty && !searched.isEmpty else { return Text(str) }
var result = Text("")
var range = str.startIndex..<str.endIndex
repeat {
guard let found = str.range(of: searched, options: .caseInsensitive, range: range, locale: nil) else {
result = result + Text(str[range])
break
}
let prefix = str[range.lowerBound..<found.lowerBound]
result = result + Text(prefix) + Text(str[found]).bold().foregroundColor(.yellow)
range = found.upperBound..<str.endIndex
} while (true)
return result
}
How to use Attributed String in SwiftUI
iOS 15 and Swift 5.5
Text
now supports markdown and also you can create custom attributes:
You can even get defined attributes remotely like:
iOS 13 and 14
You can combine multiple Text
objects together with a simple +
operator and that will handle some of the attributions:
Each one can have multiple and specific modifiers
A fully supported fallback!
Since it doesn't support directly on Text
(till iOS 15), you can bring the UILabel
there and modify it in anyway you like:
Implementation:
struct UIKLabel: UIViewRepresentable {
typealias TheUIView = UILabel
fileprivate var configuration = { (view: TheUIView) in }
func makeUIView(context: UIViewRepresentableContext<Self>) -> TheUIView { TheUIView() }
func updateUIView(_ uiView: TheUIView, context: UIViewRepresentableContext<Self>) {
configuration(uiView)
}
}
Usage:
var body: some View {
UIKLabel {
$0.attributedText = NSAttributedString(string: "HelloWorld")
}
}
Highlight a specific part of the text in SwiftUI
iOS 13, Swift 5. There is a generic solution described in this medium article. Using it you can highlight any text anywhere with the only catch being it cannot be more then 64 characters in length, since it using bitwise masks.
https://medium.com/@marklucking/an-interesting-challenge-with-swiftui-9ebb26e77376
This is the basic code in the article.
ForEach((0 ..< letter.count), id: \.self) { column in
Text(letter[column])
.foregroundColor(colorCode(gate: Int(self.gate), no: column) ? Color.black: Color.red)
.font(Fonts.futuraCondensedMedium(size: fontSize))
}
And this one to mask the text...
func colorCode(gate:Int, no:Int) -> Bool {
let bgr = String(gate, radix:2).pad(with: "0", toLength: 16)
let bcr = String(no, radix:2).pad(with: "0", toLength: 16)
let binaryColumn = 1 << no - 1
let value = UInt64(gate) & UInt64(binaryColumn)
let vr = String(value, radix:2).pad(with: "0", toLength: 16)
print("bg ",bgr," bc ",bcr,vr)
return value > 0 ? true:false
}
Adding unlimited lines in a Text (SwiftUI)
For wrapping Text in a Form .lineLimit(Int.max)
did not work for me. It seems there needs to be some width for it to know when to wrap. I believe the official way is with .fixedSize
:
Text(message)
.fixedSize(horizontal: false, vertical: true)
The documentation states:
This example shows the effect of fixedSize(horizontal:vertical:) on a text view that is wider than its parent, preserving the ideal, untruncated width of the text view.
https://developer.apple.com/documentation/swiftui/view/fixedsize(horizontal:vertical:)
The text doesn't get wrapped in swift UI
Try changing the second Text's lineLimit
to a number instead of nil:
VStack(alignment: .leading) {
Text("SwiftUI is a modern way to declare user interfaces for any Apple platform. ")
.font(.title)
.color(.red)
.lineLimit(nil)
Text("Create beautiful, dynamic apps faster than ever before.")
.font(.system(size: 20))
.lineLimit(2)
}.padding(EdgeInsets(top: 0, leading: 10, bottom: 0, trailing: 10))
Result:
Related Topics
Media Query Not Working for iPhone and Ipad
Tabbarcontroller Adding Custom Button Not Clickable Issue
How to Make One Side of a Div Pointy with CSS
How to Set Gradient Background Transparency on iOS Safari
Checking Cellular Network Type in iOS
How to Add Pagination in UItableview.
Notification in Swift Every Day at a Set Time
Bluetooth Didconnect or Didfailtoconnect Not Called Xcode 9.2 with iOS 11.1.2
Are Private Frameworks Supported on iOS
How to Make a Phonegap App for iOS Without Mac
How to Add a Lock Screen Widget (Requiring iOS 16) and Still Support iOS 15
Random Glitchy Rendering of Svg on iOS/Wkwebview
How to Select Table View Row Selection with Custom Checkbox Button
Get Apple Watch Heartratevariabilitysdnn Realtime
Initialization of 'Unsafepointer<Int>' Results in a Dangling Pointer