xcode怎么新建html,如何从xcode提供html文件的值

我不是100%肯定我明白了这个问题,但我认为你是问你是否可以从加载HTML文件您的应用程序包,然后从您的应用程序动态插入一些不同的HTML内容...对吧?

如果是这样,你当然可以做到这一点。

首先,我们将内容加载到可变字符串:

NSString *htmlPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"combo.html"];

NSError *error;

NSMutableString *htmlContent = [NSMutableString stringWithContentsOfFile: htmlPath

encoding: NSUTF8StringEncoding

error: &error];

要插入在组合框中选择一个新的选项/值,使用这样的:

// look for the start of the combo box called "food"

NSRange range = [htmlContent rangeOfString: @"select name=\"food\""

options: NSCaseInsensitiveSearch];

if (range.location != NSNotFound) {

// search for the end tag

range.length = htmlContent.length - range.location;

NSRange end = [htmlContent rangeOfString: @""

options: NSCaseInsensitiveSearch

range: range];

if (end.location != NSNotFound) {

NSString *newChoice = @"Awesome!"; // get this dynamically however you want

NSString *newOption =

[NSString stringWithFormat: @"%@\n", newChoice];

[htmlContent insertString: newOption atIndex: end.location];

NSLog(@"htmlContent = %@", htmlContent);

}

}

如果要更改显示的值在一个现有的组合框选项,然后使用如下代码:

NSString *optionTwoHtml = @"";

NSRange optionTwo = [htmlContent rangeOfString: optionTwoHtml

options: NSCaseInsensitiveSearch];

if (optionTwo.location != NSNotFound) {

int start = optionTwo.location + optionTwoHtml.length;

// search for the end tag

optionTwo.length = htmlContent.length - optionTwo.location;

NSRange end = [htmlContent rangeOfString: @""

options: NSCaseInsensitiveSearch

range: optionTwo];

if (end.location != NSNotFound) {

NSString *newValue = @"Better Than Best!";

NSRange oldRange = NSMakeRange(start, end.location - start);

[htmlContent replaceCharactersInRange: oldRange

withString: newValue];

NSLog(@"htmlContent = %@", htmlContent);

}

}

注意这个代码只是显示你可以做什么。它没有针对性能进行优化。您展示的HTML内容非常小,因此解析代码的效率无关紧要。如果您使用更大的HTML文件,您可能需要优化一些。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值