How to get the width of an NSString?

Here’s a relatively simple approach. Just create an NSAttributedString with the appropriate font and ask for its size: – (CGFloat)widthOfString:(NSString *)string withFont:(NSFont *)font { NSDictionary *attributes = [NSDictionary dictionaryWithObjectsAndKeys:font, NSFontAttributeName, nil]; return [[[NSAttributedString alloc] initWithString:string attributes:attributes] size].width; }

NSString : easy way to remove UTF-8 accents from a string?

NSString *str = @”Être ou ne pas être. C’était là-bas.”; NSData *data = [str dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; NSString *newStr = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]; NSLog(@”%@”, newStr); … or try using NSUTF8StringEncoding instead. List of encoding types here: https://developer.apple.com/documentation/foundation/nsstringencoding Just FTR here’s a one line way to write this great answer: yourString = [[NSString alloc] initWithData: [yourString … Read more

How to get substring of NSString?

Option 1: NSString *haystack = @”value:hello World:value”; NSString *haystackPrefix = @”value:”; NSString *haystackSuffix = @”:value”; NSRange needleRange = NSMakeRange(haystackPrefix.length, haystack.length – haystackPrefix.length – haystackSuffix.length); NSString *needle = [haystack substringWithRange:needleRange]; NSLog(@”needle: %@”, needle); // -> “hello World” Option 2: NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@”^value:(.+?):value$” options:0 error:nil]; NSTextCheckingResult *match = [regex firstMatchInString:haystack options:NSAnchoredSearch range:NSMakeRange(0, haystack.length)]; NSRange needleRange … Read more

Using NSRegularExpression to extract URLs on the iPhone

The method matchesInString:options:range: returns an array of NSTextCheckingResult objects. You can use fast enumeration to iterate through the array, pull out the substring of each match from your original string, and add the substring to a new array. NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@”http?://([-\\w\\.]+)+(:\\d+)?(/([\\w/_\\.]*(\\?\\S+)?)?)?” options:NSRegularExpressionCaseInsensitive error:&error]; NSArray *arrayOfAllMatches = [regex matchesInString:httpLine options:0 range:NSMakeRange(0, [httpLine length])]; NSMutableArray … Read more

How to create a hex color string UIColor initializer in Swift? [duplicate]

Xcode 9 • Swift 4 or later extension UIColor { convenience init?(hexaRGB: String, alpha: CGFloat = 1) { var chars = Array(hexaRGB.hasPrefix(“#”) ? hexaRGB.dropFirst() : hexaRGB[…]) switch chars.count { case 3: chars = chars.flatMap { [$0, $0] } case 6: break default: return nil } self.init(red: .init(strtoul(String(chars[0…1]), nil, 16)) / 255, green: .init(strtoul(String(chars[2…3]), nil, 16)) … Read more

With what should I replace the deprecated sizeWithFont: method?

After an hour of trial error I managed to make it work: CGSize maximumLabelSize = CGSizeMake(tableView.width, MAXFLOAT); NSStringDrawingOptions options = NSStringDrawingTruncatesLastVisibleLine | NSStringDrawingUsesLineFragmentOrigin; NSDictionary *attr = @{NSFontAttributeName: [UIFont systemFontOfSize:15]}; CGRect labelBounds = [string boundingRectWithSize:maximumLabelSize options:options attributes:attr context:nil]; Update: As Mr. T mentions in answer below : In iOS 7 and later, this method returns fractional … Read more

Find all locations of substring in NSString (not just first)

You can use rangeOfString:options:range: and set the third argument to be beyond the range of the first occurrence. For example, you can do something like this: NSRange searchRange = NSMakeRange(0,string.length); NSRange foundRange; while (searchRange.location < string.length) { searchRange.length = string.length-searchRange.location; foundRange = [string rangeOfString:substring options:0 range:searchRange]; if (foundRange.location != NSNotFound) { // found an occurrence … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)