nsstring

iOS归档解档

て烟熏妆下的殇ゞ 提交于 2019-11-29 06:36:49
归档与解档是iOS中序列化与反序列化的方式,需要实现 encodeWithCoder 和 initWithCoder 方法,实现方式有两种:第一种是分别为属性赋值;第二种是通过runtime机制,循环为属性赋值。 新建Teacher类 @interface Teacher : NSObject @property (nonatomic, strong) NSString *grade;//年级 @property (nonatomic, assign) NSInteger studentCount;//学生人数 @end 调用序列化与反序列化方法 - (void)save { Teacher *t = [[Teacher alloc] init]; t.grade = @"1年3班"; t.studentCount = 40; NSString *plistFilePath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"t.data"]; [NSKeyedArchiver archiveRootObject:t toFile:plistFilePath]; } - (void)get { NSString *plistFilePath = [NSTemporaryDirectory()

Objective-C – Replace newline sequences with one space

匆匆过客 提交于 2019-11-29 06:28:25
How can I replace newline (\n) sequences with one space. I.e the user has entered a double newline ("\n\n") I want that replaced with one space (" "). Or the user has entered triple newlines ("\n\n\n") I want that replaced with also one space (" "). Try this: NSArray *split = [orig componentsSeparatedByCharactersInSet:[NSCharacterSet newlineCharacterSet]]; split = [split filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"length > 0"]]; NSString *res = [split componentsJoinedByString:@" "]; This is how it works: First line splits by newline characters Second line removes empty items

how to split strings in objective c

て烟熏妆下的殇ゞ 提交于 2019-11-29 06:26:52
How to split a string in objective-C? I am working on an short application that contains a date picker. I do display date get it from date picker and display it through a label. My main question is that how can I split the the date in to three separate strings? any one has idea about it? Thanks You could use -[NSString componentsSeparatedByString:] or NSScanner to split the string, or use NSCalendar to extract the pieces of the date you're interested in. Use something like [yourString componentsSeparatedByString:@"/"] . You will get an NSArray of separated strings. If you can format date into

iOS中计算字符串NSString的高度

风格不统一 提交于 2019-11-29 06:25:00
根据固定宽度计算字符串高度: 1 NSString *info = @"但是公司的高度是广东省公司的广东省高速度来开个大帅哥多撒谎个爱好就跟他说噶三公司噶是的刚好是我哥如果黑暗如果坏都干撒降低公司及嘎斯进欧冠赛欧结果就赛欧国际韶关;可垃圾费;阿尔加两块;三个身高萨嘎干撒的公司的高度上收到公司的公司都给ID搜狗破is打个屁偶是东莞IP手动皮革是滴哦苹果是滴哦苹果度搜皮为欧公司的漂漂是第三个是干撒噶是的噶虽然刚撒旦个撒公司的公司的高度";; 2 CGFloat textW = CONVER_VALUE(345.0f) - CONVER_VALUE(15.0f) * 2; 3 CGRect infoRect = [info boundingRectWithSize:CGSizeMake(textW, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:CONVER_VALUE(13.0f)]} context:nil]; 4 NSLog(@"字符串高度是:%f",ceil(infoRect.size.height)); 来源: https://www.cnblogs.com/liuzhi20101016/p

Markdown to NSAttributedString library? [closed]

♀尐吖头ヾ 提交于 2019-11-29 06:14:29
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 2 years ago . Now that NSAttributedString is fully supported in iOS 6, is there a library that will take an NSString with markdown, and convert it to NSAttributedString? 回答1: I've just added an NSString to NSAttributedString lightweight markup parser to MGBoxKit. It's not Markdown but it's very similar. So far it supports

WebView设置字体颜色, 字体大小,背景

青春壹個敷衍的年華 提交于 2019-11-29 06:01:47
1.第一种方式:在WebView的代理方法 webViewDidFinishLoad中: 字体大小: [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName(‘body‘)[0].style.webkitTextSizeAdjust= ‘12%‘"]; 字体颜色: [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName(‘body‘)[0].style.webkitTextFillColor= ‘green‘"]; 背景颜色: [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName(‘body‘)[0].style.background=‘#F6F7F3‘"]; 2.第一种方式:在下面可以通过改变margin大小来改变webview文字距离边距大小,13代表字体大小, self . strBookTips是要被操作的字符串。 NSString *BookStr = [ NSString stringWithFormat : @"<html> \n" "<head> \n" "

IOS webview 自适应高度和加入css样式

℡╲_俬逩灬. 提交于 2019-11-29 05:56:19
//webview 自适应高度 CGFloat height = [[_webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight"] floatValue] + 20; _webView.frame = CGRectMake(0, _heightSize, SCREEN_WIDTH, height); //加入css样式和html的设置 - ( NSString *)reSizeImageWithHTML { NSString *cssLine = @"<link rel=\"stylesheet\" href=\"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css\" integrity=\"sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u\" crossorigin=\"anonymous\"" ; NSString *htmlUrl = [ NSString stringWithFormat : @"%@%@" ,cssLine, _content ]; // NSString *htmlUrl =

How might I check if a particular NSString is present in an NSArray?

风流意气都作罢 提交于 2019-11-29 05:33:11
How might I check if a particular NSString is presnet in an NSArray? You can do it like, NSArray* yourArray = [NSArray arrayWithObjects: @"Str1", @"Str2", @"Str3", nil]; if ( [yourArray containsObject: yourStringToFind] ) { // do found } else { // do not found } Iterating or containsObject are order n ways to find. If you want constant time lookup, you can also maintain a hash table like NSSet or NSHashTable but that increases space but saves time. NSArray* strings = [NSArray arrayWithObjects: @"one", @"two", @"three", nil]; NSSet *set = [NSSet setWithArray:strings]; NSString* stringToFind = @

How to calculate height of html string?

若如初见. 提交于 2019-11-29 05:15:21
I want to display html text on a label(TTStyleLabel). I am receieving text in form of html. How do I calculate height of html string? To get height of html text you need to put that html info uiwebview. After loading the html in uiwebview you can get its height in its delegate methods like this - - (void)viewDidLoad { [super viewDidLoad]; webview.delegate = self; [webview loadHTMLString:@"<div id='foo' style='background: red'>The quick brown fox jumped over the lazy dog.</div>" baseURL:nil]; } - (void)webViewDidFinishLoad:(UIWebView *)webView { NSString *output = [webview

How to “pass on” a variable number of arguments to NSString's +stringWithFormat:

时光总嘲笑我的痴心妄想 提交于 2019-11-29 04:53:35
问题 I would like to write a function in Objective-C such as the one below, that takes a variable number of arguments, and passes those arguments on to +stringWithFormat: . I know about vsnprintf , but that would imply converting the NSString 'format' to C and back (and would also mean converting the formatting placeholders within it as well...). The code below compiles, but of course does not behave as I want :) NSString *estr(NSString *format, ...) { va_list args; va_start(args, format);