I'm trying to set a limit in my label to 10 characters, I'm a complete newbie to programming in general so its my 3rd month so far... anyway thanks in advance :-)
You can also create a String extension and reuse it with any text string.. You can do it in the following way:
create the extension:
extension String {
func maxLength(length: Int) -> String {
var str = self
let nsString = str as NSString
if nsString.length >= length {
str = nsString.substring(with:
NSRange(
location: 0,
length: nsString.length > length ? length : nsString.length)
)
}
return str
}
}
you can use the extension in the following way:
label.text = "This is a Very Long Label".maxLength(length: 10)
the previous code was tested in Swift 5.0
If you want to limit the UILabel to just 10 characters then
you just have to assign it with a text with length of 10.
You can use NSString and NSRange to extract the text you need.
let str = "This is a Very Long Label"
let nsString = str as NSString
if nsString.length >= 10
{
label.text = nsString.substringWithRange(NSRange(location: 0, length: nsString.length > 10 ? 10 : nsString.length))
}
Check out this answer: https://stackoverflow.com/a/39677331/2548473
My version is (Swift 5+):
extension String {
func shorted(to symbols: Int) -> String {
guard self.count > symbols else {
return self
}
return self.prefix(symbols) + " ..."
}
}
SWIFT 3
let str = GL_GetNews[indexPath.row]["n_body"].stringValue
let nsString = str as NSString
if nsString.length > 0
{
cell.newsDescription.text = nsString.substring(with: NSRange(location: 0, length: nsString.length > 200 ? 200 : nsString.length))
}
Related
I use this code to get the current line range:
extension UITextView {
var currentLineRange: NSRange {
let nsText = self.text as NSString
let currentRange = nsText.lineRange(for: self.selectedRange)
return currentRange
}
}
How can I get the line range before another one, like :
func getLineRangeBefore(_ lineRange: NSRange) -> NSRange {
//...
}
One way to do this is to get the line start of the range, then find the line range of the position before the line start:
extension NSString {
func getLineRangeBefore(_ lineRange: NSRange) -> NSRange? {
var lineStart = 0
getLineStart(&lineStart, end: nil, contentsEnd: nil, for: lineRange)
if lineStart == 0 {
return nil
}
return self.lineRange(for: NSRange(location: lineStart - 1, length: 0))
}
}
Note that this should return an optional NSRange because the range could be contained by the first line, in which case there is no line before it.
I get different text from API and I want change text color for every 5 first word. I try use range and attributes string, but I do something wrong and this not good work for me. How can i do it?
this is my code:
private func setMessageText(text: String) {
let components = text.components(separatedBy: .whitespacesAndNewlines)
let words = components.filter { !$0.isEmpty }
if words.count >= 5 {
let attribute = NSMutableAttributedString.init(string: text)
var index = 0
for word in words where index < 5 {
let range = (text as NSString).range(of: word, options: .caseInsensitive)
attribute.addAttribute(NSAttributedString.Key.foregroundColor, value: Colors.TitleColor, range: range)
attribute.addAttribute(NSAttributedString.Key.font, value: Fonts.robotoBold14, range: range)
index += 1
}
label.attributedText = attribute
} else {
label.text = text
}
}
enter image description here
It's more efficient to get the index of the end of the 5th word and add color and font once for the entire range.
And you are strongly discouraged from bridging String to NSString to get a subrange from a string. Don't do that. Use native Swift Range<String.Index>, there is a convenience API to convert Range<String.Index> to NSRange reliably.
private func setMessageText(text: String) {
let components = text.components(separatedBy: .whitespacesAndNewlines)
let words = components.filter { !$0.isEmpty }
if words.count >= 5 {
let endOf5thWordIndex = text.range(of: words[4])!.upperBound
let nsRange = NSRange(text.startIndex..<endOf5thWordIndex, in: text)
let attributedString = NSMutableAttributedString(string: text)
attributedString.addAttributes([.foregroundColor : Colors.TitleColor, .font : Fonts.robotoBold14], range: nsRange)
label.attributedText = attributedString
} else {
label.text = text
}
}
An alternative – more sophisticated – way is to use the dedicated API enumerateSubstrings(in:options: with option byWords
func setMessageText(text: String) {
var wordIndex = 0
var attributedString : NSMutableAttributedString?
text.enumerateSubstrings(in: text.startIndex..., options: .byWords) { (substring, substringRange, enclosingRange, stop) in
if wordIndex == 4 {
let endIndex = substringRange.upperBound
let nsRange = NSRange(text.startIndex..<endIndex, in: text)
attributedString = NSMutableAttributedString(string: text)
attributedString!.addAttributes([.foregroundColor : Colors.TitleColor, .font : Fonts.robotoBold14], range: nsRange)
stop = true
}
wordIndex += 1
}
if let attributedText = attributedString {
label.attributedText = attributedText
} else {
label.text = text
}
}
I'm setting the text on a textview and then calling this method of the UITextView extension in order to create links out of the words that are hashtags and mentions (Swift 3)
extension UITextView {
func resolveHashTags(font: UIFont = UIFont.systemFont(ofSize: 17.0)){
if let text = self.text {
let words:[String] = text.components(separatedBy: " ")
let attrs = [ NSFontAttributeName : font ]
let attrString = NSMutableAttributedString(string: text, attributes:attrs)
for word in words {
if (word.characters.count > 1 && ((word.hasPrefix("#") && word[1] != "#") || (word.hasPrefix("#") && word[1] != "#"))) {
let matchRange = text.range(of: word)
let newWord = String(word.characters.dropFirst())
if let matchRange = matchRange {
attrString.addAttribute(NSLinkAttributeName, value: "\(word.hasPrefix("#") ? "hashtag:" : "mention:")\(newWord)", range: text.NSRangeFromRange(range: matchRange))
}
}
}
self.attributedText = attrString
}
}
}
My issue is very simple. I have no way to create a link for something like this "helloworld#hello" simply because my word does not have a prefix of "#"
Another scenario I can't figure out is when the user puts multi hashtags together for example "hello world, how are you? #success#moments#connect" as this would all be considered 1 hashtag with the current logic when it should be 3 different links.
How do i correct? thank you
func resolveHashTags(text : String) -> NSAttributedString{
var length : Int = 0
let text:String = text
let words:[String] = text.separate(withChar: " ")
let hashtagWords = words.flatMap({$0.separate(withChar: "#")})
let attrs = [NSFontAttributeName : UIFont.systemFont(ofSize: 17.0)]
let attrString = NSMutableAttributedString(string: text, attributes:attrs)
for word in hashtagWords {
if word.hasPrefix("#") {
let matchRange:NSRange = NSMakeRange(length, word.characters.count)
let stringifiedWord:String = word
attrString.addAttribute(NSLinkAttributeName, value: "hash:\(stringifiedWord)", range: matchRange)
}
length += word.characters.count
}
return attrString
}
To separate words I used a string Extension
extension String {
public func separate(withChar char : String) -> [String]{
var word : String = ""
var words : [String] = [String]()
for chararacter in self.characters {
if String(chararacter) == char && word != "" {
words.append(word)
word = char
}else {
word += String(chararacter)
}
}
words.append(word)
return words
}
}
func textViewDidChange(_ textView: UITextView) {
textView.attributedText = resolveHashTags(text: textView.text)
textView.linkTextAttributes = [NSForegroundColorAttributeName : UIColor.red]
}
I hope this is what you are looking for. Tell me if it worked out for you.
If you're willing to use a 3rd party library, you could try using Mustard (disclaimer: I'm the author).
You could match hashtag tokens using this tokenizer:
struct HashtagTokenizer: TokenizerType, DefaultTokenizerType {
// start of token is identified by '#'
func tokenCanStart(with scalar: UnicodeScalar) -> Bool {
return scalar == UnicodeScalar(35) // ('35' is scalar value for the # character)
}
// all remaining characters must be letters
public func tokenCanTake(_ scalar: UnicodeScalar) -> Bool {
return CharacterSet.letters.contains(scalar)
}
}
Which can then be used to match the hashtags in the text:
let hashtags = "hello world, how are you? #success#moments#connect".tokens(matchedWith: HashtagTokenizer())
// hashtags.count -> 3
// hashtags[0].text -> "#success"
// hashtags[0].range -> 26..<34
// hashtags[1].text -> "#moments"
// hashtags[1].range -> 34..<42
// hashtags[2].text -> "#connect"
// hashtags[2].range -> 42..<50
The array returned is an array of tokens, where each one contains a text property of the matched text, and a range property of the range of that matched text in the original string that you can use to create a link on the text view.
How do I convert this to Swift:
NSString *searchString = [NSString stringWithFormat:#"%#", #"Apple_HFS "];
NSRange range = [tempString rangeOfString:searchString];
NSUInteger *idx = range.location + range.length;
Thanks
If you use String, you can just reference endIndex:
let searchString: String = "Apple_HFS "
if let range: Range<String.Index> = tempString.rangeOfString(searchString) {
let index = range.endIndex
let stringAfter = tempString.substringFromIndex(index)
// do something with `stringAfter`
} else {
// not found
}
I included the types so you could see what's going on, but generally I'd just write:
let searchString = "Apple_HFS "
if let range = tempString.rangeOfString(searchString) {
let stringAfter = tempString.substringFromIndex(range.endIndex)
// do something with `stringAfter`
} else {
// not found
}
Is that what you're looking for ?
var str : NSString = "A string that include the searched string Apple_HFS inside"
let searchString : NSString = "Apple_HFS "
let range : NSRange = str.rangeOfString(searchString) // (42, 10)
let idx : Int = range.location + range.length // 52
Demonstration : http://swiftstub.com/831969865/
Simple solution without using ANY objc methods or types:
Suppose you have:
let nsrange = NSRange(location: 3, length: 5)
let string = "My string"
And now you need to convert NSRange to Range:
let range = Range(start: advance(string.startIndex, nsrange.location), end: advance(string.startIndex, nsrange.location + nsrange.length))
So if you have an NSString that goes:
#"My blue car is bigger than my blue shoes or my blue bicycle";
I would like a method that replaces only the first instance of blue with green, to produce:
#"My green car is bigger than my blue shoes or my blue bicycle";
How does one do this?
Assuming the following inputs:
NSString *myString = #"My blue car is bigger then my blue shoes or my blue bicycle";
NSString *original = #"blue";
NSString *replacement = #"green";
The algorithm is quite simple:
NSRange rOriginal = [myString rangeOfString:original];
if (NSNotFound != rOriginal.location) {
myString = [myString stringByReplacingCharactersInRange:rOriginal withString:replacement];
}
SWIFT 3 and 4 UPDATE:
extension String
{
func stringByReplacingFirstOccurrenceOfString(
target: String, withString replaceString: String) -> String
{
if let range = self.range(of: target) {
return self.replacingCharacters(in: range, with: replaceString)
}
return self
}
}
A more Swift'y version of the implementations posted here. This one matches the syntax replacingOccurrences(of:with:) in Swift 5.1
extension String {
func replacingFirstOccurrence(of target: String, with replacement: String) -> String {
guard let range = self.range(of: target) else { return self }
return self.replacingCharacters(in: range, with: replacement)
}
}
An example of usage:
let string = "I like bacon, please give me some more bacon!"
let newString = string.replacingFirstOccurrence(of: "bacon", with: "meat")
Swift 4 version:
func replacingFirstOccurrence(of string: String, with replacement: String) -> String {
guard let range = self.range(of: string) else { return self }
return replacingCharacters(in: range, with: replacement)
}
In Swift is useful extends String in this way:
public extension String {
func stringByReplacingFirstOccurrenceOfString(target: String, withString replaceString: String) -> String {
if let range = self.rangeOfString(target) {
return self.stringByReplacingCharactersInRange(range, withString: replaceString)
}
return self
}
}
In this way you can call wherever:
let s = "The red red cat".stringByReplacingFirstOccurrenceOfString("red", withString: "fat")
print(s) // "The fat red cat"
NSString *initialString = #"My blue car is bigger then my blue shoes or my blue bicycle";
NSRange range = [initialString rangeOfString:#"blue"];
NSString *replacedString = [initialString stringByReplacingCharactersInRange:range withString:#"green"];
NSLog(#"replacedString: %#", replacedString);
First locate the substring, and then make the replacement. Example :
NSString *aString = #"foo bar foo";
NSRange firstFooRange = [aString rangeOfString:#"foo"];
NSString *anotherString = [aString stringByReplacingOccurrencesOfString:#"foo"
withString:#"bar"
options:0
range:firstFooRange];
NSString documentation.
-(NSString*) replaceFirstOccuarnceFromString: (NSString*)input withOriginal:(NSString*) original AndReplacment:(NSString*)replacement
{
NSRange rOriginal = [input rangeOfString: original];
if (NSNotFound != rOriginal.location) {
input = [input
stringByReplacingCharactersInRange: rOriginal
withString: replacement];
}
return input;
}